C語言中檔案流操作的基本函式總結
函式所在標頭檔案:stdio.h
說明:前半部分主要為對各個檔案流操作函式的例舉,後半部分著重於
上機執行分析。文中部分引用自王桂林老師的C/C++課件。
1.FIELE *fopen(const char*filename,const char *mode)
以mode的方式,開啟一個以filename(指標型別)命名的檔案,
返回一個指向該檔案緩衝區的指標,該指標是後續操作的控制代碼。

2.int fclose(FILE *stream)
fclose()用來關閉先前用fopen()開啟的檔案。並讓檔案緩衝
區的資料寫入檔案中,並釋放系統提供的檔案資源。成功範返回
0;失敗返回-1(EOF)。
示例1:
#include <stdio.h> int main() { FILE * fp = fopen("data.txt", "w"); //以"w"的方式開啟一個名為"data.txt"的檔案,檔案不存在則建立 //判斷檔案是否都開啟成功 if(fp == NULL) { printf("open error!\n"); return -1; } fputs("china is great!!", fp); //向檔案中寫入"chia is great!!" fclose(fp); //關閉檔案 return 0; } //該程式執行完畢,在該工程目錄下生成了一個"data.txt"的文字檔案,其內容為上述字串
3.int fputc(int ch,FILE *stream)
將ch字元寫入檔案,成功返回寫入字元,失敗返回-1。
int fputs(char *str,FILE *fp)
將str指向的字串寫入fp指向的檔案中,正常返回0;失敗返回1.
示例2:
#include <stdio.h> int main() { FILE* fp = fopen("data.txt","w"); if(fp == NULL) { printf("open error\n"); return -1; } char ch; for(ch = 'a'; ch<='z'; ch++) printf("%3c",fputc(ch,fp)); //將字元寫入文字的同時,將其打印出來 fclose(fp); return 0; }
程式執行結果:
同時,也在工程目錄下生成了一個叫"data.txt"的檔案,開啟檔案
可以發現其內容為a~z。
4.int fgetc(FILE *stream)
從檔案流中讀取一個字元並返回。成功返回讀取的字元;讀到檔案
末尾或失敗返回-1。
char *fgets(char *str,int length,FILE *fp)
從fp指向的檔案中,至多讀length-1個字元,送入陣列str
中,如果在讀入length-1個字元結束前遇到\n或EOF,讀入即
結束,字串讀入後在最後加一個'\0'字元。
正常返回str指標,出錯或遇到檔案結尾,返回NULL指標。
示例3:
#include <stdio.h> int main() { FILE* fp = fopen("data.txt","r"); //data.txt內容為a~z if(fp == NULL) { printf("open error\n"); return -1; } char ch; while((ch = fgetc(fp)) != EOF) printf("%3c",ch); fclose(fp); return 0; }
程式執行結果:
5.int feof(FILE *stream)
判斷檔案是否讀到末尾,未讀到末尾返回0,非零讀到末尾。一般不
用,檔案讀到結尾,再去讀一次,容易導致多讀一次。不建議使用!
6.int fread(void* buffer,int num,int count,FILE *fp)
int fwrite(void*buffer,int num,int count,FILE *fp)
將buffer指向的資料寫入fp指向的檔案中,或是把fp指向的檔案中的數
據讀到buffer中,num為每個要讀寫的欄位數的位元組數,count為要讀寫
的欄位數。成功返回讀/寫的欄位數(count);出錯或檔案結束返回0。
這兩個函式不同於其他函式,當我們試圖用fread/fwrite去讀寫文字文
件的時候,發現文字中的格式己經沒有任何意義,只是一個普通的字元。
它所進行的操作為二進位制操作,通俗來說就是對一些文字識別符號如'\0',
'\n'等已經不敏感了,這些文字識別符號都被當做成一個二進位制來讀寫。
7.void rewind(FILE *STREAM)
將檔案指標重新指向一個流的開頭。
8.int ftell(FILE *stream)
得到流檔案的當前讀寫位置,其返回值是當前讀寫位置偏離檔案頭
部的位元組數。失敗返回-1。
9.int fseek(FILE *stream,long offset,int where)
偏移檔案指標,成功返回0,失敗返回-1。where是偏移的起始位置。
//#define SEEK_CUR 1 當前位置
//#define SEEK_END 2 檔案結尾
//#define SEEK_SET 0 檔案開頭
fseek(fp,100L,0);把fp指標移動到離檔案開頭100位元組處;
fseek(fp,100L,1);把fp指標移動到離檔案當前位置100位元組處;
fseek(fp,-100,2);把fp指標退回到離檔案結尾100位元組處。
示例4:
#include <stdio.h> int main() { FILE* fp = fopen("data.txt","w+"); if(fp == NULL) { printf("open error\n"); return -1; } char ch; for( ch = 'a'; ch<='z'; ch++) { fputc(ch,fp); } rewind(fp); //此處一定不能少,否則檔案指標將指向檔案末尾,而打印不出任何東西 while((ch = fgetc(fp))&& !feof(fp)) { printf("%3c",ch); } fclose(fp); return 0; }
程式執行結果:
示例5:
#include <stdio.h> int main() { FILE* fp = fopen("data.txt","w+"); if(fp == NULL) { printf("open error\n"); return -1; } fputs("abcd\nefg",fp); rewind(fp); char ch; while((ch = fgetc(fp)) != EOF) printf("%c",ch); fclose(fp); return 0; }
程式執行結果:
示例6:
下面是一個很有意思的程式,請判斷下fgets()共執行了多少次;
先別看答案,相信這個理解了,這部分問題就不會太大,下面直接
上程式:
#include <stdio.h> int main() { FILE* fp = fopen("data.txt","r"); if(fp == NULL) { printf("open error\n"); return -1; } char buf[10] = {0}; int count = 0; while(fgets(buf,10,fp) != NULL)//注意,即使沒遇到結束符,每次也只能讀9個字元 { printf("%s",buf); count++; } fclose(fp); putchar(10); printf("+++++++++%d++++++++++",count); return 0; }
其中data.txt是這樣的:
程式執行結果:
從執行結果來看,似乎是執行了 5 次,但仔細看看,真的是5次嗎?
實際上, 每讀取一次,判斷返回的指標是否為空 ,若不為空,則count加
1,若為空,則不執行迴圈體。因此,實際上是讀取了 6 次, 只是最後一
次讀取後發現到了檔案末尾,因此返回來一個空指標,判斷為空指標
沒執行count++操作。 值得注意的是fgets()每次讀取的字元數。
示例7:
#include <stdio.h> int main() { FILE* fp = fopen("data.txt","w+"); if(fp == NULL) { printf("open error\n"); return -1; } fputs("china is great!!",fp); int i = ftell(fp); printf("%d\n",i); //列印當前指標位置離檔案頭的位元組數 rewind(fp); //讓檔案指標指向檔案頭 i = ftell(fp); printf("%d\n",i); char buf[20] = {0}; fgets(buf,20,fp); printf("%s\n",buf); fclose(fp); return 0; }
程式執行結果:
示例8:
#include <stdio.h>
int main()
{
FILE* fp = fopen("data.txt","w+");
if(fp == NULL)
{
printf("open error\n");
return -1;
}
fputs("china is great!!",fp);
fseek(fp,2,0);
//將檔案指標偏移到離檔案頭兩個位元組的位置
char buf[20];
fgets(buf,20,fp);
printf("%s\n",buf);
fclose(fp);
return 0;
}
程式執行結果:

示例9:
#include <stdio.h> #include <string.h> int main() { FILE *fpw = fopen("bin.txt","wb+"); if(fpw == NULL) return -1; char *p = "china \n is \0 great"; fwrite(p,1,strlen(p)+7,fpw); //將指標的內容寫入到檔案中,由於strlen()所求長度 //遇到0截止,因此需將0後面的元素補上。 rewind(fpw); char buf[1024]; int n = fread(buf,1,1024,fpw); //每次讀取一個位元組,讀取1024次,返回讀取的欄位數(此處為位元組數) //遇到檔案末尾或讀取錯誤返回0 printf("%d\n",n); int i = 0; for(i = 0;i<n;i++) printf("%c",buf[i]); fclose(fpw); return 0; }
程式執行結果: