1. 程式人生 > >C語言檔案操作詳解

C語言檔案操作詳解

* 檔案狀態檢查

A. 檔案結束
(1) 函式原型

int feof(FILE *fp)

(2) 功能說明
   該函式用來判斷檔案是否結束。
(3) 引數說明
   fp:檔案指標。
(4) 返回值
   0:假值,表示檔案未結束。
   1:真值,表示檔案結束。
(5) 例項

【例8.10】

  1. #include <stdio.h>
  2. void main(int argc,char *argv[])  
  3. {  
  4.     FILE *in,*out;  
  5.     char ch;  
  6.     if(argc!=3)  
  7.     {  
  8.         printf("Usage: copyfile filename1 filename2\n");  
  9.         return;  
  10.     }  
  11.     if((in=fopen(argv[1],"rb"))==NULL)  
  12.     {  
  13.         printf("The file %s can not be opened.\n",argv[1]);  
  14.         return;  
  15.     }  
  16.     if((out=fopen(argv[2],"wb"))==NULL)  
  17.     {  
  18.         printf("The file %s can not be opened.\n"
    ,argv[2]);  
  19.         return;  
  20.     }  
  21.     while(!feof(in))  
  22.     {  
  23.         ch=fgetc(in);  
  24.         if(ferror(in))  
  25.         {  
  26.             printf("read error!\n");  
  27.             clearerr(in);  
  28.         }  
  29.         else
  30.         {  
  31.             fputc(ch,out);  
  32.             if(ferror(out))  
  33.             {  
  34.                 printf("write error!\n");  
  35.                 clearerr(out);  
  36.             }  
  37.         }  
  38.     }  
  39.     fclose(in);  
  40.     fclose(out);  
  41. }  


B. 檔案讀/寫出錯
(1) 函式原型

int ferror(FILE *fp)

(2) 功能說明
   檢查由fp指定的檔案在讀寫時是否出錯。
(3) 引數說明
   fp:檔案指標。
(4) 返回值
   0:假值,表示無錯誤。
   1:真值,表示出錯。

C. 清除檔案錯誤標誌

(1) 函式原型

void clearerr(FILE *fp)

(2) 功能說明
   清除由fp指定檔案的錯誤標誌。
(3) 引數說明
   fp:檔案指標。
(4) 返回值
   無。
(5) 例項

【例8.12】

  1. #include <stdio.h>
  2. void main(int argc,char *argv[])  
  3. {  
  4.     FILE *in,*out;  
  5.     char ch;  
  6.     if(argc!=3)  
  7.     {  
  8.         printf("Usage: copyfile filename1 filename2\n");  
  9.         return;  
  10.     }  
  11.     if((in=fopen(argv[1],"rb"))==NULL)  
  12.     {  
  13.         printf("The file %s can not be opened.\n",argv[1]);  
  14.         return;  
  15.     }  
  16.     if((out=fopen(argv[2],"wb"))==NULL)  
  17.     {  
  18.         printf("The file %s can not be opened.\n",argv[2]);  
  19.         return;  
  20.     }  
  21.     while(!feof(in))  
  22.     {  
  23.         ch=fgetc(in);  
  24.         if(ferror(in))  
  25.         {  
  26.             printf("read error!\n");  
  27.             clearerr(in);  
  28.         }  
  29.         else
  30.         {  
  31.             fputc(ch,out);  
  32.             if(ferror(out))  
  33.             {  
  34.                 printf("write error!\n");  
  35.                 clearerr(out);  
  36.             }  
  37.         }  
  38.     }  
  39.     fclose(in);  
  40.     fclose(out);  
  41. }  


D. 瞭解檔案指標的當前位置
(1) 函式原型

long ftell(FILE *fp)

(2) 功能說明
   取得由fp指定檔案的當前讀/寫位置,該位置值用相對於檔案開頭的位移量來表示。
(3) 引數說明
   fp:檔案指標。
(4) 返回值
   正常返回:位移量(這是個長整數)。
   異常返回:-1,表示出錯。
(5) 例項

* 檔案定位

A. 反繞
(1) 函式原型

void rewind(FILE *fp)

(2) 功能說明
   使由檔案指標fp指定的檔案的位置指標重新指向檔案的開頭位置。
(3) 引數說明
   fp:檔案指標。
(4) 返回值
   無。
(5) 例項

【例8.14】

  1. #include <stdio.h>
  2. void main()  
  3. {  
  4.     FILE *in,*out;  
  5.     in=fopen("filename1","r");  
  6.     out=fopen("filename2","w");  
  7.     while(!feof(in)) fputc(fgetc(in),out);  
  8.     rewind(out);  
  9.     while(!feof(in)) putchar(fgetc(in));  
  10.     fclose(in);  
  11.     fclose(out);  
  12. }  

B. 隨機定位
(1) 函式原型