(文章目錄)
前言
接上一篇 C語言檔案操作<一>
一、檔案的順序讀寫函數
fgetc和fputc
-
fgetc
字元輸入函數 适用于所有輸入流
-
fputc
字元輸出函數 适用于所有輸出流
int fgetc( FILE *stream );
int fputc( int c, FILE *stream );
- fputc 寫檔案
int main()
{
FILE* pfWrite = fopen("test.txt", "w");
if (pfWrite == NULL)//檢驗
{
printf("%s\n", strerror(errno));//如果寫檔案錯誤列印錯誤原因
return 0;
}
//寫檔案
fputc("a", pfWrite);
fputc("b", pfWrite);
fputc("c", pfWrite);
//關閉檔案
fclose(pfWrite);
pfWrite == NULL;
return 0;
}
- fgetc 讀檔案
int main()
{
FILE* pfRead = fopen("test.txt", "r");
if (pfRead == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//讀檔案
printf("%s", fgetc(pfRead));//a
printf("%s", fgetc(pfRead));//b
printf("%s", fgetc(pfRead));//c
//關閉檔案
fclose(pfRead);
pfRead == NULL;
return 0;
}
fgets和fputs
- fges 文本行輸入函數 适用于所有輸入流
- fputs 文本行輸入函數 适用于所有輸出流
兩個函數的功能:
//Get a string from a stream. 從流中擷取字元串。
char *fgets(char *string, int n, FILE *stream);
//Write a string to a stream. 将字元串寫入流。
int fputs(const char *string, FILE *stream);
用fgets函數讀取一行:
int main()
{
char buff[100] = { 0 };
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
return 0;
}
//讀檔案
fgets(buff, 100, pf);
printf("%s", buff); //注意buff裡面本身就有一個換行符 (檔案裡面末尾有換行符)
//打開成功,讀檔案,關閉檔案
fclose(pf);
pf == NULL;
return 0;
}
讀取一行列印到顯示器上 如圖:

用fputs函數寫一行:
int main()
{
char buff[100] = { 0 };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
return 0;
}
//寫檔案
fputs("hello\n", pf);
fputs("word\n", pf);
//打開成功,寫檔案,關閉檔案
fclose(pf);
pf = NULL;
return 0;
}
用fputs 寫一行到檔案裡面:如下:
fgets和fputs也可以操作鍵盤和螢幕
鍵盤輸入abcd 回車 螢幕列印 abcd
fscanf 和 fprintf
- fscanf 格式化輸入函數 适用于所有輸入流
- fprintf 格式化輸出函數 适用于所有輸出流
簡單對比 scanf 和 fscanf,printf 和 fprintf,用法都非常相似
int scanf( const char *format [,argument]... );
int fscanf( FILE *stream, const char *format [, argument ]... );
int printf( const char *format [, argument]... );
int fprintf( FILE *stream, const char *format [, argument ]...);
fprintf 寫檔案
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = { 100, 3.14, "abc" };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
return 0;
}
//格式化的形式寫檔案
fprintf(pf, "%d %f %s", s.n, s.score, s.arr);
fclose(pf);
pf = NULL;
return 0;
}
fscanf 讀檔案
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = { 100, 3.14, "abc" };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
return 0;
}
//格式化的形式輸入資料
fscanf(pf, "%d %f %s", s.n, s.score, s.arr);
fclose(pf);
pf = NULL;
return 0;
}
當然fscanf和fprintf函數同樣可以運用于标準的輸入輸出流(stdin,stdout)
對比一組函數
- scanf/fscanf/sscanf
- ptintf/fprintf/sprintf
scanf/printf:是針對标準輸入流/标準輸出流的 格式化輸入/輸出語句
fscnaf/fprintf:是針對所有輸入流/所有輸出流的 格式化輸入/輸出語句
sscanf/sprintf:sscanf是從字元串中讀取格式化的資料。sprintf是把格式化資料輸出成(存儲到)字元串
fraed 和 fwriite
- fread 二進制輸入函數 适用于 檔案
- fwrite 二進制輸出函數 适用于 檔案
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
struct S{
char name[20];
int age;
double score;
};
int main()
{
struct S s = { "張三", 20, 55.6 };
FILE* pf = fopen("test.txt", "wb");
if (pf == NULL)
{
return 0;
}
//二進制形式寫檔案
fwrite(&s, sizeof(struct S), 1, pf);
fclose(pf);
pf = NULL;
return 0;
}
fread 讀檔案 ,fwrite 寫檔案 。兩個函數用法類似
二、檔案的随機讀寫函數
前面介紹的函數都是順序讀寫函數 ,有時候我們需要從某一個位置開始讀寫函數。下面的這些函數可以實作其功能。
fseek 函數
根據檔案指針的位置和偏移量來定位檔案指針
Moves the file pointer to a specified location.
int fseek( FILE *stream, long offset, int origin );
//三個參數所指的内容:pf 偏移量 檔案指針的目前位置
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
return 0;
}
//定位檔案指針
fseek(pf,2,SEEK_CUR);//偏移量為2
//讀取檔案
int ch = fgetc(pf);
prinf("%c\n",ch);
fclose(pf);
pf = NULL;
return 0;
}
如圖從檔案指針的目前位置 a 前面開始偏移量為2,列印c
-
SEEK_CUR
Current position of file pointer (檔案指針的目前位置)
-
SEEK_END (檔案的末尾位置)
End of file
-
SEEK_SET (檔案起始位置)
Beginning of file
三、關于檔案結束的判定
我們知道:EOF --> end of file 檔案結束的标志
如果一個檔案裡面什麼都沒有我們打開檔案讀到的就是 “-1”,檔案結束的位置有一個EOF存在。
-
牢記:在檔案讀取過程中,不能用feof函數的傳回值直接來判定檔案的是否結束。
而是應用于當檔案讀取結束的時候,判斷是讀取失敗結束,還是遇到檔案尾EOF使檔案結束。
-
文本檔案是否讀取結束,判斷傳回值是否為EOF或則NULL
例如:fgetc判斷是否為EOF fgets判斷傳回值是否為NULL
-
二進制檔案的讀取結束判斷,判斷傳回值是否小于實際要讀的個數
例如:fread 判斷傳回值是否小于實際要讀的個數