格式化讀寫檔案fprintf,fscanf

2021-07-23 17:25:37 字數 993 閱讀 5722

#define _crt_secure_no_warnings

#include #include #include /*

int fprintf(file * stream, const char * format, ...);

功能:根據引數format字串來轉換並格式化資料,然後將結果輸出到stream指定的檔案中,指定出現字串結束符 '\0' 為止。

引數:stream:已經開啟的檔案

format:字串格式,用法和printf()一樣

返回值:

成功:實際寫入檔案的字元個數

失敗:-1

int fscanf(file * stream, const char * format, ...);

功能:從stream指定的檔案讀取字串,並根據引數format字串來轉換並格式化資料。

引數:stream:已經開啟的檔案

format:字串格式,用法和scanf()一樣

返回值:

成功:實際從檔案中讀取的字元個數

失敗: - 1

fscanf 遇到空格或者遇到換行結束

*/const char *filename = "./a.txt";

void test()

fprintf(fp, "hello world!\n%s", "hello obama!\n");

fclose(fp);

fp = null;

fp = fopen(filename, "r");

if (fp == null)

char buf[1024] = ;

while (!feof(fp))

printf("%s", buf);//輸出helloworld!helloobama! 沒有換行和空格

memset(buf, 0, 1024);

} fclose(fp);

}void main()

C語言格式化讀寫檔案

fscanf 和 fprintf 函式與前面使用的 scanf 和 printf 功能相似,都是格式化讀寫函式,兩者的區別在於 fscanf 和 fprintf 的讀寫物件不是鍵盤和顯示器,而是磁碟檔案。這兩個函式的原型為 int fscanf file fp,char format,int fpr...

C語言格式化讀寫檔案

fscanf 和fprintf 函式與前面使用的 scanf 和printf 功能相似,都是格式化讀寫函式,兩者的區別在於 fscanf 和fprintf 的讀寫物件不是鍵盤和顯示器,而是磁碟檔案。這兩個函式的原型為 int fscanf file fp,char format,int fprint...

python 讀寫檔案格式化輸出

python讀寫檔案 1.open file object open thefile.txt try all the text file object.read finally file object.close 2.讀檔案 讀文字檔案 input open data r 第二個引數預設為r inp...