fprintf fscanf等函式的用法

2021-04-12 14:49:14 字數 1785 閱讀 6935

對檔案的讀和寫是最常用的檔案操作。在c語言中提供了多種檔案讀寫的函式:

·字元讀寫函式 :fgetc和fputc

·字串讀寫函式:fgets和fputs

·資料塊讀寫函式:fread和fwrite

·格式化讀寫函式:fscanf和fprinf

隨著每次資料的讀取,檔案流指標fp都作相應的移動

使用以上函式都要求包含標頭檔案stdio.h。例子都來自msdn

1 fprintf——print formatted data to a stream

#include

#include

file *stream;

void main( void )

output

this is a string

101.500000

2 fscanf——read formatted data from a stream
#include file *stream;

void main( void )

}output

a-string

65000

3.141590

x

3 fread——reads data from a stream
4 fwrite——writes data to a stream
讀資料塊函式呼叫的一般形式為: 

fread(buffer,size,count,fp);

寫資料塊函式呼叫的一般形式為:

fwrite(buffer,size,count,fp);

其中:

buffer 是乙個指標,在fread函式中,它表示存放輸入資料的首位址。在fwrite函式中,它表示存放輸出資料的首位址。

size 表示資料塊的位元組數。

count 表示要讀寫的資料塊塊數。

fp 表示檔案指標。

5 fgets 沒有看出與fread太大的區別,除了fread可以處理string外的其他不同檔案的資料型別
6 fputs
7 fgetc fputs
從鍵盤輸入一行字元,寫入乙個檔案,再把該檔案內容讀出顯示在螢幕上。 #i nclude 

main()

file *fp;

char ch;

if((fp=fopen("d://jrzh//example//string","wt+"))==null)

printf("cannot open file strike any key exit!");

getch();

exit(1);

printf("input a string:/n");

ch=getchar();

while (ch!='/n')

fputc(ch,fp);

ch=getchar();

rewind(fp); //repositions the file pointer to the beginning of a file

ch=fgetc(fp);

while(ch!=eof)

putchar(ch);

ch=fgetc(fp);

printf("/n");

fclose(fp);

hive grouping sets 等聚合函式

函式說明 grouping sets 在乙個 group by 查詢中,根據不同的維度組合進行聚合,等價於將不同維度的 group by 結果集進行 union all cube 根據 group by 的維度的所有組合進行聚合 rollup 是 cube 的子集,以最左側的維度為主,從該維度進行層...

C memcpy memmove等記憶體操作函式

返回值 destination 官方說明 複製記憶體塊 將num bytes的值從源指向的位置直接複製到目標指向的記憶體塊。源指標和目標指標指向的物件的基本型別與此函式無關 結果是資料的二進位制副本。函式不檢查源中是否有任何終止的空字元 它總是精確地複製num位元組。為了避免溢位,目標引數和源引數指...

sql中nvl,cast,power等常用函式

1 nvl 表示式1,表示式2 如果1為空,則該函式取表示式2的值 如 b nvl a,0 如果a值為空,b取0 2 nvl 表示式1,表示式2,表示式3 如果1有值,取2,如果1為空,取表示式3的值 總之 如果1為空,都取最後乙個引數的值 3 power用法 power a,b 這是求冪次方,a的...