天天看點

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);

}

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);

fscanf

功能:從一個流中執行格式化輸入

函數原型:int fscanf(file stream, char format[,argument…]);

char 格式化輸出函數,和scanf裡的格式一樣

fscanf(fp,"%s",str);

clearerr(清除檔案流的錯誤旗标)

相關函數 feof

定義函數 void clearerr(file * stream);

函數說明 clearerr()清除參數stream指定的檔案流所使用的錯誤旗标。

傳回值

 

4.fclose(關閉檔案)

相關函數 close,fflush,fopen,setbuf

定義函數 int fclose(file * stream);

函數說明 fclose()用來關閉先前fopen()打開的檔案。此動作會讓緩沖區内的資料寫入檔案中,并釋放系統所提供的檔案資源。

傳回值 若關檔案動作成功則傳回0,有錯誤發生時則傳回eof并把錯誤代碼存到errno。

錯誤代碼 ebadf表示參數stream非已打開的檔案。

範例 請參考fopen()。

5.fdopen(将檔案描述詞轉為檔案指針)

相關函數 fopen,open,fclose

定義函數 file * fdopen(int fildes,const char * mode);

函數說明 fdopen()會将參數fildes 的檔案描述詞,轉換為對應的檔案指針後傳回。參數mode 字元串則代表着檔案指針的流形态,此形态必須和原先檔案描述詞讀寫模式相同。關于mode 字元串格式請參考fopen()。

傳回值 轉換成功時傳回指向該流的檔案指針。失敗則傳回null,并把錯誤代碼存在errno中。

file * fp =fdopen(0,”w+”);

fprintf(fp,”%s/n”,”hello!”);

執行 hello!

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

相關函數 fopen,fgetc,fgets,fread

定義函數 int feof(file * stream);

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

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

7.fflush(更新緩沖區)

相關函數 write,fopen,fclose,setbuf

定義函數 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則表示到了檔案尾。

file *fp;

int c;

fp=fopen(“exist”,”r”);

while((c=fgetc(fp))!=eof)

printf(“%c”,c);

9.fgets(由檔案中讀取一字元串)

定義函數 har * fgets(char * s,int size,file * stream);

函數說明 fgets()用來從參數stream所指的檔案内讀入字元并存到參數s所指的記憶體空間,直到出現換行字元、讀到檔案尾或是已讀了size-1個字元為止,最後會加上null作為字元串結束。

傳回值 gets()若成功則傳回s指針,傳回null則表示有錯誤發生。

char s[80];

fputs(fgets(s,80,stdin),stdout);

執行 this is a test /輸入/

this is a test /輸出/

10.fileno(傳回檔案流所使用的檔案描述詞)

相關函數 open,fopen

定義函數 int fileno(file * stream);

函數說明 fileno()用來取得參數stream指定的檔案流所使用的檔案描述詞。

傳回值 傳回檔案描述詞。

int fd;

fp=fopen(“/etc/passwd”,”r”);

fd=fileno(fp);

printf(“fd=%d/n”,fd);

執行 fd=3

12.fputc(将一指定字元寫入檔案流中)

相關函數 fopen,fwrite,fscanf,putc

定義函數 int fputc(int c,file * stream);

函數說明 fputc 會将參數c 轉為unsigned char 後寫入參數stream 指定的檔案中。

傳回值 fputc()會傳回寫入成功的字元,即參數c。若傳回eof則代表寫入失敗。

char a[26]=”abcdefghijklmnopqrstuvwxyz”;

int i;

fp= fopen(“noexist”,”w”);

for(i=0;i<26;i++)

fputc(a,fp);

13.fputs(将一指定的字元串寫入檔案内)

相關函數 fopen,fwrite,fscanf,fputc,putc

定義函數 int fputs(const char * s,file * stream);

函數說明 fputs()用來将參數s所指的字元串寫入到參數stream所指的檔案内。

傳回值 若成功則傳回寫出的字元個數,傳回eof則表示有錯誤發生。

範例 請參考fgets()。

fread(從檔案流讀取資料)

相關函數 fopen,fwrite,fseek,fscanf

定義函數 size_t fread(void * ptr,size_t size,size_t nmemb,file * stream);

函數說明 fread()用來從檔案流中讀取資料。參數stream為已打開的檔案指針,參數ptr 指向欲存放讀取進來的資料空間,讀取的字元數以參數size*nmemb來決定。fread()會傳回實際讀取到的nmemb數目,如果此值比參數nmemb 來得小,則代表可能讀到了檔案尾或有錯誤發生,這時必須用feof()或ferror()來決定發生什麼情況。

傳回值 傳回實際讀取到的nmemb數目。

附加說明

#define nmemb 3

struct test

char name[20];

int size;

}s[nmemb];

int main(){

file * stream;

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]=6

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

name[2]=windows2000 size[2]=11

14.freopen(打開檔案)

相關函數 fopen,fclose

定義函數 file * freopen(const char * path,const char * mode,file * stream);

函數說明 參數path字元串包含欲打開的檔案路徑及檔案名,參數mode請參考fopen()說明。參數stream為已打開的檔案指針。freopen()會将原stream所打開的檔案流關閉,然後打開參數path的檔案。

傳回值 檔案順利打開後,指向該流的檔案指針就會被傳回。如果檔案打開失敗則傳回null,并把錯誤代碼存在errno 中。

fp=freopen(“/etc/group”,”r”,fp);

15.fseek(移動檔案流的讀寫位置)

相關函數 rewind,ftell,fgetpos,fsetpos,lseek

定義函數 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允許負值的出現。

下列是較特别的使用方式:

欲将讀寫位置移動到檔案開頭時:fseek(file *stream,0,seek_set);

欲将讀寫位置移動到檔案尾時:fseek(file *stream,0,0seek_end);

傳回值 當調用成功時則傳回0,若有錯誤則傳回-1,errno會存放錯誤代碼。

附加說明 fseek()不像lseek()會傳回讀寫位置,是以必須使用ftell()來取得目前讀寫的位置。

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));

執行 offset = 5

offset =0

offset=10

16.ftell(取得檔案流的讀取位置)

相關函數 fseek,rewind,fgetpos,fsetpos

定義函數 long ftell(file * stream);

函數說明 ftell()用來取得檔案流目前的讀寫位置。參數stream為已打開的檔案指針。

傳回值 當調用成功時則傳回目前的讀寫位置,若有錯誤則傳回-1,errno會存放錯誤代碼。

錯誤代碼 ebadf 參數stream無效或可移動讀寫位置的檔案流。

範例 參考fseek()。

17.fwrite(将資料寫至檔案流)

相關函數 fopen,fread,fseek,fscanf

定義函數 size_t fwrite(const void * ptr,size_t size,size_t nmemb,file * stream);

函數說明 fwrite()用來将資料寫入檔案流中。參數stream為已打開的檔案指針,參數ptr 指向欲寫入的資料位址,總共寫入的字元數以參數size*nmemb來決定。fwrite()會傳回實際寫入的nmemb數目。

傳回值 傳回實際寫入的nmemb數目。

#define set_s (x,y) {strcoy(s[x].name,y);s[x].size=strlen(y);}

set_s(0,”linux!”);

set_s(1,”freebsd!”);

set_s(2,”windows2000.”);

stream=fopen(“/tmp/fwrite”,”w”);

fwrite(s,sizeof(struct test),nmemb,stream);

執行 參考fread()。

18.getc(由檔案中讀取一個字元)

相關函數 read,fopen,fread,fgetc

定義函數 int getc(file * stream);

函數說明 getc()用來從參數stream所指的檔案中讀取一個字元。若讀到檔案尾而無資料時便傳回eof。雖然getc()與fgetc()作用相同,但getc()為宏定義,非真正的函數調用。

範例 參考fgetc()。

19.getchar(由标準輸入裝置内讀進一字元)

相關函數 fopen,fread,fscanf,getc

定義函數 int getchar(void);

函數說明 getchar()用來從标準輸入裝置中讀取一個字元。然後将該字元從unsigned char轉換成int後傳回。

傳回值 getchar()會傳回讀取到的字元,若傳回eof則表示有錯誤發生。

附加說明 getchar()非真正函數,而是getc(stdin)宏定義。

int c,i;

for(i=0li<5;i++)

c=getchar();

putchar©;

執行 1234 /輸入/

1234 /輸出/

20.gets(由标準輸入裝置内讀進一字元串)

相關函數 fopen,fread,fscanf,fgets

定義函數 char * gets(char *s);

函數說明 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>

char template[ ]=”template-xxxxxx”;

mktemp(template);

printf(“template=%s/n”,template);

22.putc(将一指定字元寫入檔案中)

相關函數 fopen,fwrite,fscanf,fputc

定義函數 int putc(int c,file * stream);

函數說明 putc()會将參數c轉為unsigned char後寫入參數stream指定的檔案中。雖然putc()與fputc()作用相同,但putc()為宏定義,非真正的函數調用。

傳回值 putc()會傳回寫入成功的字元,即參數c。若傳回eof則代表寫入失敗。

範例 參考fputc()。

23.putchar(将指定的字元寫到标準輸出裝置)

定義函數 int putchar (int c);

函數說明 putchar()用來将參數c字元寫到标準輸出裝置。

傳回值 putchar()會傳回輸出成功的字元,即參數c。若傳回eof則代表輸出失敗。

附加說明 putchar()非真正函數,而是putc(c,stdout)宏定義。

範例 參考getchar()。

24.rewind(重設檔案流的讀寫位置為檔案開頭)

相關函數 fseek,ftell,fgetpos,fsetpos

定義函數 void rewind(file * stream);

函數說明 rewind()用來把檔案流的讀寫位置移至檔案開頭。參數stream為已打開的檔案指針。此函數相當于調用fseek(stream,0,seek_set)。

範例 參考fseek()

25.setbuf(設定檔案流的緩沖區)

相關函數 setbuffer,setlinebuf,setvbuf

定義函數 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

定義函數 void setbuffer(file * stream,char * buf,size_t size);

函數說明 在打開檔案流後,讀取内容之前,調用setbuffer()可用來設定檔案流的緩沖區。參數stream為指定的檔案流,參數buf指向自定的緩沖區起始位址,參數size為緩沖區大小。

27.setlinebuf(設定檔案流為線性緩沖區)

相關函數 setbuffer,setbuf,setvbuf

定義函數 void setlinebuf(file * stream);

函數說明 setlinebuf()用來設定檔案流以換行為依據的無緩沖io。相當于調用:setvbuf(stream,(char * )null,_iolbf,0);請參考setvbuf()。

28.setvbuf(設定檔案流的緩沖區)

相關函數 setbuffer,setlinebuf,setbuf

定義函數 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

定義函數 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);

if (fp==null){

printf(“fail to open file\n”);

// fseek(fp,-1,seek_end);

num = ftell(fp);

printf(“test file long:%d\n”,num);

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);

sprintf(str,“xiewei test is:%s”, “abcdefghigkmni”);

printf(“str2=%s\n”, str);

// fprintf(fp,"%s\n",str);

fwrite(str,2,10,fp);

if(str!=null){

free(str);

return 0;

網絡上志同道合,我們一起學習網絡安全,一起進步,qq群:694839022