C語言檔案操作函數大全(超詳細)

來源:互聯網
上載者:User
fopen(開啟檔案)
相關函數 open,fclose
表標頭檔 #include<stdio.h>
定義函數 FILE * fopen(const char * path,const char * mode);
函數說明 參數path字串包含欲開啟的檔案路徑及檔案名稱,參數mode字串則代表著流形態。
mode有下列幾種形態字串:
r 開啟唯讀檔案,該檔案必須存在。
r+ 開啟可讀寫的檔案,該檔案必須存在。
w 開啟唯寫檔案,若檔案存在則檔案長度清為0,即該檔案內容會消失。若檔案不存在則建立該檔案。
w+ 開啟可讀寫檔案,若檔案存在則檔案長度清為零,即該檔案內容會消失。若檔案不存在則建立該檔案。
a 以附加的方式開啟唯寫檔案。若檔案不存在,則會建立該檔案,如果檔案存在,寫入的資料會被加到檔案尾,即檔案原先的內容會被保留。
a+ 以附加方式開啟可讀寫的檔案。若檔案不存在,則會建立該檔案,如果檔案存在,寫入的資料會被加到檔案尾後,即檔案原先的內容會被保留。

r      Open text file for reading.  The stream is positioned at the beginning of the file.r+     Open for reading and writing.  The stream is positioned at the beginning of the file.w      Truncate file to zero length or create text file for writing.  The stream is positioned at the beginning of the file.w+     Open for reading and writing.  The file is created if it does not exist, otherwise it is truncated.  The  stream  is  posi‐       tioned at the beginning of the file.a      Open  for  appending  (writing at end of file).  The file is created if it does not exist.  The stream is positioned at the       end of the file.a+     Open for reading and appending (writing at end of file).  The file is created if it does not exist.  The initial file posi‐       tion for reading is at the beginning of the file, but output is always appended to the end of the file.

上述的形態字串都可以再加一個b字元,如rb、w+b或ab+等組合,加入b 字元用來告訴函數庫開啟的檔案為二進位檔案,而非純文字檔案。不過在POSIX系統,包含Linux都會忽略該字元。由fopen()所建立的新檔案會具有S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666)許可權,此檔案許可權也會參考umask值。
傳回值 檔案順利開啟後,指向該流的檔案指標就會被返回。若果檔案開啟失敗則返回NULL,並把錯誤碼存在errno 中。
附加說明 一般而言,開檔案後會作一些檔案讀取或寫入的動作,若開檔案失敗,接下來的讀寫動作也無法順利進行,所以在fopen()後請作錯誤判斷及處理。
範例

#include<stdio.h>main(){FILE * fp;fp=fopen(“noexist”,”a+”);if(fp= =NULL) return;fclose(fp);}

1. fprintf
功能:傳送格式化輸出到一個檔案中
表標頭檔:#include<stdio.h>
函數原型:int fprintf(FILE *stream, char *format[, argument,...]);
FILE* 一個FILE型的指標
char* 格式化輸入函數,和printf裡的格式一樣
傳回值:成功時返迴轉換的位元組數,失敗時返回一個負數
fp = fopen("/local/test.c","a+");
fprintf(fp,"%s\n",str);

2. fscanf
功能:從一個流中執行格式化輸入
表標頭檔:#include<stdio.h>
函數原型:int fscanf(FILE *stream, char *format[,argument...]);
FILE* 一個FILE型的指標
char* 格式化輸出函數,和scanf裡的格式一樣
傳回值:成功時返迴轉換的位元組數,失敗時返回一個負數
fp = fopen("/local/test.c","a+");
fscanf(fp,"%s",str);

3. clearerr(清除檔案流的錯誤旗標)
相關函數 feof
表標頭檔 #include<stdio.h>
定義函數 void clearerr(FILE * stream);
函數說明 clearerr()清除參數stream指定的檔案流所使用的錯誤旗標。
傳回值
 
4.fclose(關閉檔案)
相關函數 close,fflush,fopen,setbuf
表標頭檔 #include<stdio.h>
定義函數 int fclose(FILE * stream);
函數說明 fclose()用來關閉先前fopen()開啟的檔案。此動作會讓緩衝區內的資料寫入檔案中,並釋放系統所提供的檔案資源。
傳回值 若關檔案動作成功則返回0,有錯誤發生時則返回EOF並把錯誤碼存到errno。
錯誤碼 EBADF表示參數stream非已開啟的檔案。
範例 請參考fopen()。
 
5.fdopen(將檔案描述詞轉為檔案指標)
相關函數 fopen,open,fclose
表標頭檔 #include<stdio.h>
定義函數 FILE * fdopen(int fildes,const char * mode);
函數說明 fdopen()會將參數fildes 的檔案描述詞,轉換為對應的檔案指標後返回。參數mode 字串則代表著檔案指標的流形態,此形態必須和原先檔案描述詞讀寫入模式相同。關於mode 字串格式請參考fopen()。
傳回值 轉換成功時返回指向該流的檔案指標。失敗則返回NULL,並把錯誤碼存在errno中。
範例

#include<stdio.h>main(){FILE * fp =fdopen(0,”w+”);fprintf(fp,”%s/n”,”hello!”);fclose(fp);}執行 hello!

6.feof(檢查檔案流是否讀到了檔案尾)
相關函數 fopen,fgetc,fgets,fread
表標頭檔 #include<stdio.h>
定義函數 int feof(FILE * stream);
函數說明 feof()用來偵測是否讀取到了檔案尾,尾數stream為fopen()所返回之檔案指標。如果已到檔案尾則返回非零值,其他情況返回0。
傳回值 返回非零值代表已到達檔案尾。
 
7.fflush(更新緩衝區)
相關函數 write,fopen,fclose,setbuf
表標頭檔 #include<stdio.h>
定義函數 int fflush(FILE* stream);
函數說明 fflush()會強迫將緩衝區內的資料寫回參數stream指定的檔案中。如果參數stream為NULL,fflush()會將所有開啟的檔案資料更新。
傳回值 成功返回0,失敗返回EOF,錯誤碼存於errno中。
錯誤碼 EBADF 參數stream 指定的檔案未被開啟,或開啟狀態為唯讀。其它錯誤碼參考write()。
 
8.fgetc(由檔案中讀取一個字元)
相關函數 open,fread,fscanf,getc
表標頭檔 include<stdio.h>
定義函數 nt fgetc(FILE * stream);
函數說明 fgetc()從參數stream所指的檔案中讀取一個字元。若讀到檔案尾而無資料時便返回EOF。
傳回值 getc()會返回讀取到的字元,若返回EOF則表示到了檔案尾。
範例

#include<stdio.h>main(){FILE *fp;int c;fp=fopen(“exist”,”r”);while((c=fgetc(fp))!=EOF)printf(“%c”,c);fclose(fp);}

9.fgets(由檔案中讀取一字串)
相關函數 open,fread,fscanf,getc
表標頭檔 include<stdio.h>
定義函數 har * fgets(char * s,int size,FILE * stream);
函數說明 fgets()用來從參數stream所指的檔案內讀入字元並存到參數s所指的記憶體空間,直到出現換行字元、讀到檔案尾或是已讀了size-1個字元為止,最後會加上NULL作為字串結束。
傳回值 gets()若成功則返回s指標,返回NULL則表示有錯誤發生。
範例

#include<stdio.h>main(){char s[80];fputs(fgets(s,80,stdin),stdout);}執行 this is a test /*輸入*/this is a test /*輸出*/

10.fileno(返迴文件流所使用的檔案描述詞)
相關函數 open,fopen
表標頭檔 #include<stdio.h>
定義函數 int fileno(FILE * stream);
函數說明 fileno()用來取得參數stream指定的檔案流所使用的檔案描述詞。
傳回值 返迴文件描述詞。
範例

#include<stdio.h>main(){FILE * fp;int fd;fp=fopen(“/etc/passwd”,”r”);fd=fileno(fp);printf(“fd=%d/n”,fd);fclose(fp);}執行 fd=3

12.fputc(將一指定字元寫入檔案流中)
相關函數 fopen,fwrite,fscanf,putc
表標頭檔 #include<stdio.h>
定義函數 int fputc(int c,FILE * stream);
函數說明 fputc 會將參數c 轉為unsigned char 後寫入參數stream 指定的檔案中。
傳回值 fputc()會返回寫入成功的字元,即參數c。若返回EOF則代表寫入失敗。
範例

#include<stdio.h>main(){FILE * fp;char a[26]=”abcdefghijklmnopqrstuvwxyz”;int i;fp= fopen(“noexist”,”w”);for(i=0;i<26;i++)fputc(a,fp);fclose(fp);}

13.fputs(將一指定的字串寫入檔案內)
相關函數 fopen,fwrite,fscanf,fputc,putc
表標頭檔 #include<stdio.h>
定義函數 int fputs(const char * s,FILE * stream);
函數說明 fputs()用來將參數s所指的字串寫入到參數stream所指的檔案內。
傳回值 若成功則返回寫出的字元個數,返回EOF則表示有錯誤發生。
範例 請參考fgets()。
fread(從檔案流讀取資料)
相關函數 fopen,fwrite,fseek,fscanf
表標頭檔 #include<stdio.h>
定義函數 size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream);
函數說明 fread()用來從檔案流中讀取資料。參數stream為已開啟的檔案指標,參數ptr 指向欲存放讀取進來的資料空間,讀取的字元數以參數size*nmemb來決定。Fread()會返回實際讀取到的nmemb數目,如果此值比參數nmemb 來得小,則代表可能讀到了檔案尾或有錯誤發生,這時必須用feof()或ferror()來決定發生什麼情況。
傳回值 返回實際讀取到的nmemb數目。
附加說明
範例

#include<stdio.h>#define nmemb 3struct test{char name[20];int size;}s[nmemb];int main(){FILE * stream;int i;stream = fopen(“/tmp/fwrite”,”r”);fread(s,sizeof(struct test),nmemb,stream);fclose(stream);for(i=0;i<nmemb;i++)printf(“name[%d]=%-20s:size[%d]=%d/n”,i,s.name,i,s.size);}執行name[0]=Linux! size[0]=6name[1]=FreeBSD! size[1]=8name[2]=Windows2000 size[2]=11

14.freopen(開啟檔案)
相關函數 fopen,fclose
表標頭檔 #include<stdio.h>
定義函數 FILE * freopen(const char * path,const char * mode,FILE * stream);
函數說明 參數path字串包含欲開啟的檔案路徑及檔案名稱,參數mode請參考fopen()說明。參數stream為已開啟的檔案指標。Freopen()會將原stream所開啟的檔案流關閉,然後開啟參數path的檔案。
傳回值 檔案順利開啟後,指向該流的檔案指標就會被返回。如果檔案開啟失敗則返回NULL,並把錯誤碼存在errno 中。
範例

#include<stdio.h>main(){FILE * fp;fp=fopen(“/etc/passwd”,”r”);fp=freopen(“/etc/group”,”r”,fp);fclose(fp);}

15.fseek(移動檔案流的讀寫位置)
相關函數 rewind,ftell,fgetpos,fsetpos,lseek
表標頭檔 #include<stdio.h>
定義函數 int fseek(FILE * stream,long offset,int whence);
函數說明 fseek()用來移動檔案流的讀寫位置。參數stream為已開啟的檔案指標,參數offset為根據參數whence來移動讀寫位置的位移數。
參數 whence為下列其中一種:
SEEK_SET從距檔案開頭offset位移量為新的讀寫位置。SEEK_CUR 以目前的讀寫位置往後增加offset個位移量。
SEEK_END將讀寫位置指向檔案尾後再增加offset個位移量。
當whence值為SEEK_CUR 或SEEK_END時,參數offset允許負值的出現。
下列是較特別的使用方式:
1) 欲將讀寫位置移動到檔案開頭時:fseek(FILE *stream,0,SEEK_SET);
2) 欲將讀寫位置移動到檔案尾時:fseek(FILE *stream,0,0SEEK_END);
傳回值 當調用成功時則返回0,若有錯誤則返回-1,errno會存放錯誤碼。
附加說明 fseek()不像lseek()會返回讀寫位置,因此必須使用ftell()來取得目前讀寫的位置。
範例

#include<stdio.h>main(){FILE * stream;long offset;fpos_t pos;stream=fopen(“/etc/passwd”,”r”);fseek(stream,5,SEEK_SET);printf(“offset=%d/n”,ftell(stream));rewind(stream);fgetpos(stream,&pos);printf(“offset=%d/n”,pos);pos=10;fsetpos(stream,&pos);printf(“offset = %d/n”,ftell(stream));fclose(stream);}執行 offset = 5offset =0offset=10

16.ftell(取得檔案流的讀取位置)
相關函數 fseek,rewind,fgetpos,fsetpos
表標頭檔 #include<stdio.h>
定義函數 long ftell(FILE * stream);
函數說明 ftell()用來取得檔案流目前的讀寫位置。參數stream為已開啟的檔案指標。
傳回值 當調用成功時則返回目前的讀寫位置,若有錯誤則返回-1,errno會存放錯誤碼。
錯誤碼 EBADF 參數stream無效或可移動讀寫位置的檔案流。
範例 參考fseek()。
 
17.fwrite(將資料寫至檔案流)
相關函數 fopen,fread,fseek,fscanf
表標頭檔 #include<stdio.h>
定義函數 size_t fwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream);
函數說明 fwrite()用來將資料寫入檔案流中。參數stream為已開啟的檔案指標,參數ptr 指向欲寫入的資料地址,總共寫入的字元數以參數size*nmemb來決定。Fwrite()會返回實際寫入的nmemb數目。
傳回值 返回實際寫入的nmemb數目。
範例

#include<stdio.h>#define set_s (x,y) {strcoy(s[x].name,y);s[x].size=strlen(y);}#define nmemb 3struct test{char name[20];int size;}s[nmemb];main(){FILE * stream;set_s(0,”Linux!”);set_s(1,”FreeBSD!”);set_s(2,”Windows2000.”);stream=fopen(“/tmp/fwrite”,”w”);fwrite(s,sizeof(struct test),nmemb,stream);fclose(stream);}執行 參考fread()。

18.getc(由檔案中讀取一個字元)
相關函數 read,fopen,fread,fgetc
表標頭檔 #include<stdio.h>
定義函數 int getc(FILE * stream);
函數說明 getc()用來從參數stream所指的檔案中讀取一個字元。若讀到檔案尾而無資料時便返回EOF。雖然getc()與fgetc()作用相同,但getc()為宏定義,非真正的函數調用。
傳回值 getc()會返回讀取到的字元,若返回EOF則表示到了檔案尾。
範例 參考fgetc()。
 
19.getchar(由標準輸入裝置內讀進一字元)
相關函數 fopen,fread,fscanf,getc
表標頭檔 #include<stdio.h>
定義函數 int getchar(void);
函數說明 getchar()用來從標準輸入裝置中讀取一個字元。然後將該字元從unsigned char轉換成int後返回。
傳回值 getchar()會返回讀取到的字元,若返回EOF則表示有錯誤發生。
附加說明 getchar()非真正函數,而是getc(stdin)宏定義。
範例

#include<stdio.h>main(){FILE * fp;int c,i;for(i=0li<5;i++){c=getchar();putchar(c);}}執行 1234 /*輸入*/1234 /*輸出*/

20.gets(由標準輸入裝置內讀進一字串)
相關函數 fopen,fread,fscanf,fgets
表標頭檔 #include<stdio.h>
定義函數 char * gets(char *s);
函數說明 gets()用來從標準裝置讀入字元並存到參數s所指的記憶體空間,直到出現換行字元或讀到檔案尾為止,最後加上NULL作為字串結束。
傳回值 gets()若成功則返回s指標,返回NULL則表示有錯誤發生。
附加說明 由於gets()無法知道字串s的大小,必須遇到換行字元或檔案尾才會結束輸入,因此容易造成緩衝溢出的安全性問題。建議使用fgets()取代。
範例 參考fgets()
 
21.mktemp(產生唯一的臨時檔案名稱)
相關函數 tmpfile
表標頭檔 #include<stdlib.h>
定義函數 char * mktemp(char * template);
函數說明 mktemp()用來產生唯一的臨時檔案名稱。參數template所指的檔案名稱字串中最後六個字元必須是XXXXXX。產生後的檔案名稱會借字串指標返回。
傳回值 檔案順利開啟後,指向該流的檔案指標就會被返回。如果檔案開啟失敗則返回NULL,並把錯誤碼存在errno中。
附加說明 參數template所指的檔案名稱字串必須聲明為數組,如:
char template[ ]=”template-XXXXXX”;
不可用char * template=”template-XXXXXX”;
範例

#include<stdlib.h>main(){char template[ ]=”template-XXXXXX”;mktemp(template);printf(“template=%s/n”,template);}

22.putc(將一指定字元寫入檔案中)
相關函數 fopen,fwrite,fscanf,fputc
表標頭檔 #include<stdio.h>
定義函數 int putc(int c,FILE * stream);
函數說明 putc()會將參數c轉為unsigned char後寫入參數stream指定的檔案中。雖然putc()與fputc()作用相同,但putc()為宏定義,非真正的函數調用。
傳回值 putc()會返回寫入成功的字元,即參數c。若返回EOF則代表寫入失敗。
範例 參考fputc()。
 
23.putchar(將指定的字元寫到標準輸出裝置)
相關函數 fopen,fwrite,fscanf,fputc
表標頭檔 #include<stdio.h>
定義函數 int putchar (int c);
函數說明 putchar()用來將參數c字元寫到標準輸出裝置。
傳回值 putchar()會返回輸出成功的字元,即參數c。若返回EOF則代表輸出失敗。
附加說明 putchar()非真正函數,而是putc(c,stdout)宏定義。
範例 參考getchar()。
 
24.rewind(重設檔案流的讀寫位置為檔案開頭)
相關函數 fseek,ftell,fgetpos,fsetpos
表標頭檔 #include<stdio.h>
定義函數 void rewind(FILE * stream);
函數說明 rewind()用來把檔案流的讀寫位置移至檔案開頭。參數stream為已開啟的檔案指標。此函數相當於調用fseek(stream,0,SEEK_SET)。
傳回值
範例 參考fseek()

25.setbuf(設定檔案流的緩衝區)
相關函數 setbuffer,setlinebuf,setvbuf
表標頭檔 #include<stdio.h>
定義函數 void setbuf(FILE * stream,char * buf);
函數說明 在開啟檔案流後,讀取內容之前,調用setbuf()可以用來設定檔案流的緩衝區。參數stream為指定的檔案流,參數buf指向自定的緩衝區起始地址。如果參數buf為NULL指標,則為無緩衝IO。Setbuf()相當於調用:setvbuf(stream,buf,buf?_IOFBF:_IONBF,BUFSIZ)
傳回值
 
26.setbuffer(設定檔案流的緩衝區)
相關函數 setlinebuf,setbuf,setvbuf
表標頭檔 #include<stdio.h>
定義函數 void setbuffer(FILE * stream,char * buf,size_t size);
函數說明 在開啟檔案流後,讀取內容之前,調用setbuffer()可用來設定檔案流的緩衝區。參數stream為指定的檔案流,參數buf指向自定的緩衝區起始地址,參數size為緩衝區大小。
傳回值

27.setlinebuf(設定檔案流為線性緩衝區)
相關函數 setbuffer,setbuf,setvbuf
表標頭檔 #include<stdio.h>
定義函數 void setlinebuf(FILE * stream);
函數說明 setlinebuf()用來設定檔案流以換行為依據的無緩衝IO。相當於調用:setvbuf(stream,(char * )NULL,_IOLBF,0);請參考setvbuf()。
傳回值

28.setvbuf(設定檔案流的緩衝區)
相關函數 setbuffer,setlinebuf,setbuf
表標頭檔 #include<stdio.h>
定義函數 int setvbuf(FILE * stream,char * buf,int mode,size_t size);
函數說明 在開啟檔案流後,讀取內容之前,調用setvbuf()可以用來設定檔案流的緩衝區。參數stream為指定的檔案流,參數buf指向自定的緩衝區起始地址,參數size為緩衝區大小,參數mode有下列幾種
_IONBF 無緩衝IO
_IOLBF 以換行為依據的無緩衝IO
_IOFBF 完全無緩衝IO。如果參數buf為NULL指標,則為無緩衝IO。
傳回值

29.ungetc(將指定字元寫迴文件流中)
相關函數 fputc,getchar,getc
表標頭檔 #include<stdio.h>
定義函數 int ungetc(int c,FILE * stream);
函數說明 ungetc()將參數c字元寫回參數stream所指定的檔案流。這個寫回的字元會由下一個讀取檔案流的函數取得。
傳回值 成功則返回c 字元,若有錯誤則返回EOF。

#include <stdio.h>#include <stdlib.h>int main(){     FILE *fp = NULL;     char* str;     char re;      int num = 10;     str = (char*)malloc(100);     //snprintf(str, 10,"test: %s", "0123456789012345678");    // printf("str=%s\n", str);     fp = fopen("/local/test.c","a+");     if (fp==NULL){        printf("Fail to open file\n");     }//     fseek(fp,-1,SEEK_END);     num = ftell(fp);     printf("test file long:%d\n",num);     fscanf(fp,"%s",str);     printf("str = %s\n",str);     printf("test a: %s\n",str);     while ((re=getc(fp))!=EOF){//getc可以用作fgetc用        printf("%c",re);     }     //fread(str,10,10,fp);     fgets(str,100,fp);     printf("test a: %s\n",str);     sprintf(str,"xiewei test is:%s", "ABCDEFGHIGKMNI");     printf("str2=%s\n", str);   //  fprintf(fp,"%s\n",str);     fwrite(str,2,10,fp);     num = ftell(fp);     if(str!=NULL){        free(str);     }     fclose(fp);     return 0;}
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.