C語言超詳細檔案操作函式大全

本文是本站小編搜尋整理的關於對C語言中的檔案操作函式進行了詳細的總結分析,供參考學習,希望對大家有所幫助!想了解更多相關資訊請持續關注我們應屆畢業生考試網!

C語言超詳細檔案操作函式大全

  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,"%sn",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指定的檔案流所使用的錯誤旗標。

返回值

  se(關閉檔案)

相關函式 close,fflush,fopen,setbuf

表頭檔案 #include<stdio.h>

定義函式 int fclose(FILE * stream);

函式說明 fclose()用來關閉先前fopen()開啟的檔案。此動作會讓緩衝區內的資料寫入檔案中,並釋放系統所提供的檔案資源。

返回值 若關檔案動作成功則返回0,有錯誤發生時則返回EOF並把錯誤程式碼存到errno。

錯誤程式碼 EBADF表示引數stream非已開啟的檔案。

範例 請參考fopen()。

  en(將檔案描述詞轉為檔案指標)

相關函式 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!

  (檢查檔案流是否讀到了檔案尾)

相關函式 fopen,fgetc,fgets,fread

表頭檔案 #include<stdio.h>

定義函式 int feof(FILE * stream);

函式說明 feof()用來偵測是否讀取到了檔案尾,尾數stream為fopen()所返回之檔案指標。如果已到檔案尾則返回非零值,其他情況返回0。

返回值 返回非零值代表已到達檔案尾。

  sh(更新緩衝區)

相關函式 write,fopen,fclose,setbuf

表頭檔案 #include<stdio.h>

定義函式 int fflush(FILE* stream);

函式說明 fflush()會強迫將緩衝區內的資料寫回引數stream指定的檔案中。如果引數stream為NULL,fflush()會將所有開啟的檔案資料更新。

返回值 成功返回0,失敗返回EOF,錯誤程式碼存於errno中。

錯誤程式碼 EBADF 引數stream 指定的檔案未被開啟,或開啟狀態為只讀。其它錯誤程式碼參考write()。

  c(由檔案中讀取一個字元)

相關函式 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);

}

  s(由檔案中讀取一字串)

相關函式 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 /*輸出*/

  no(返回檔案流所使用的檔案描述詞)

相關函式 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

  c(將一指定字元寫入檔案流中)

相關函式 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);

}

  s(將一指定的字串寫入檔案內)

相關函式 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 3

struct 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,,i,);

}

執行

name[0]=Linux! size[0]=6

name[1]=FreeBSD! size[1]=8

name[2]=Windows2000 size[2]=11

  pen(開啟檔案)

相關函式 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);

}