C語言-文件操作詳解


C文件概述

1、所謂“文件”是指一組相關數據的有序集合。

2、這個數據集合有一個名稱,叫做文件名

3、文件通常是駐留在外部介質(如磁盤等)上的,在使用時才調入內存中來

4、操作系統是以文件為單位對數據進行管理的

 

文件分類

1、從用戶觀點:

  a) 特俗文件(標准輸入輸出文件或標准設備文件)

  b) 普通文件(磁盤文件)

2、從操作系統的角度看,每個與主機相連的輸入、輸出設備看作是一個文件

3、按數據的組織形式:

  a) ASCII文件(文本文件):每一個字節放一個ASCII代碼

b) 二進制文件:把內存中的數據按其在內存中的存儲形式原樣輸出到磁盤上存放

 

C語言對文件的處理方法

緩沖文件系統:系統自動地在內存區為每一個正在使用的文件開辟一個緩沖區。用緩沖文件系統進行的輸入輸出又稱為高級磁盤輸入輸出。

非緩沖文件系統:系統不自動開辟確定大小的緩沖區,而由程序為每個文件設定緩沖區。用非緩沖文件系統進行的輸入輸出又稱為低級輸入輸出系統。

 

文件的打開與關閉

1、文件的打開(fopen函數)

2、文件的關閉(fclose函數)

函數功能:使文件指針變量不指向該文件,也就是文件指針變量與文件“脫鈎”,此后不能再通過該指針對原來與其相聯系的文件進行讀寫操作

返回值:關閉成功返回值為0;否則返回EOF-1

 

使用文件的方式共有12種,下面給出了它們的符號和意義。

 文件使用方式

             意義

   “r”

   只讀打開一個文本文件,只允許讀數據

   “w”

   只寫打開或建立一個文本文件,只允許寫數據

   “a”

   追加打開一個文本文件,並在文件末尾寫數據

   “rb”

   只讀打開一個二進制文件,只允許讀數據

   “wb”

   只寫打開或建立一個二進制文件,只允許寫數據

   “ab”

   追加打開一個二進制文件,並在文件末尾寫數據

   “r+”

  讀寫打開一個文本文件,允許讀和寫

   “w+”

  讀寫打開或建立一個文本文件,允許讀寫

   “at+”

  讀寫打開一個文本文件,允許讀,或在文件末追加數據

   “rb+”

  讀寫打開一個二進制文件,允許讀和寫

   “wb+”

  讀寫打開或建立一個二進制文件,允許讀和寫

   “ab+”

  讀寫打開一個二進制文件,允許讀,或在文件末追加數據

 

 

文件的讀寫

字符讀寫函數: fgetc 和 fputc

1、在文件內部有一個位置指針,用來指向文件的當前讀寫字節

2、在文件打開時,該指針總是指向文件的第一個字節,只用fgetc函數后,該位置指針將向后移動一個字節,因此可連續多次使用fgetc函數,讀取多個字符

3、文件指針和未見內部的位置指針不是一回事

4、文件指針是指向整個文件的,須在程序中定義說明,只要不重新賦值,文件指針的值是不變的

5、文件內部的位置指針用以知識文件內部的當前讀寫位置,每讀寫一次,該指針均向后移動,它不需要在程序中定義說明,而是由系統自動設置的

 

fputc(ch, fp);

函數功能: 將字符(ch的值)輸出到fp所指向的文件中去

 

/*

2011092121 5459 

功能:

字符讀寫函數: fgetc 和 fputc

fputc(ch, fp);

函數功能: 將字符(ch的值)輸出到fp所指向的文件中去

*/

#include <stdio.h>

#include <stdlib.h>

 

void main()

{

FILE * fp;

char ch, filename[20];

 

printf("Please input the filename you want to write:");

scanf("%s", filename);

 

if(!(fp = fopen(filename, "wt++"))) //  文件的打開(fopen函數);  wt+” 讀寫打開或建立一個文本文件,允許讀寫

{

printf("Cannot open the file!\n");

exit(0);//終止程序

}

 

printf("Please input the sentences you want to write:");

ch = getchar(); 

ch = getchar();

while( ch != EOF) //EOF為文件結束

{

fputc(ch, fp); //fputc(ch, fp); 函數功能: 將字符(ch的值)輸出到fp所指向的文件中去

ch = getchar();

}

 

fclose(fp);

}

 

 

 

/*

2011092122 2329 

*/

#include <stdio.h>

#include <stdlib.h>

 

void main()

{

FILE * fp;

char ch, filename[20];

 

printf("Please input the filename you want to write:");

scanf("%s", filename);

 

if(!(fp = fopen(filename, "r"))) //  文件的打開(fopen函數);  wt+” 讀寫打開或建立一個文本文件,允許讀寫

{

printf("Cannot open the file!\n");

exit(0);//終止程序

}

 

while( ch != EOF) //EOF為文件結束 EOF 也就等價於 -1 表示讀到文件末尾了 或者說 是 文件的結束符

{

ch = fgetc(fp);

putchar(ch); //putchar();用於把ch顯示在屏幕上

}

 

fclose(fp);

}

 

 

/*

2011092513 0329 

功能:

完成文本文件的復制

*/

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

FILE *in, *out;

 

char infile[10];

char outfile[10];

char c;

 

printf("enter the infile name:\n");

scanf("%s", infile);

 

printf("enter the outfile name:\n");

scanf("%s", outfile);

 

if( (in=fopen(infile, "r")) == NULL ) //   r 只讀打開一個文本文件,只允許讀數據

exit(0);

 

if( (out=fopen(outfile, "w")) == NULL ) //   w” 只寫打開或建立一個文本文件,只允許寫數據

exit(0);

 

c=fgetc(in); //fgetc(), getc() 從指定文件取得一個字符

while(!feof(in)) //文件狀態 feof() 若到文件末尾,函數值為真

{

fputc(c, out); //fputc(), putc() 把字符輸出到指定文件

c=fgetc(in);

}

 

fclose(in);

fclose(out);

 

return 0;

}

 

 

/*

2011092513 1003 

功能:

完成二進制文件的復制

*/

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

FILE *in, *out;

 

char infile[10];

char outfile[10];

char c;

 

printf("enter the infile name:\n");

scanf("%s", infile);

 

printf("enter the outfile name:\n");

scanf("%s", outfile);

 

if( (in=fopen(infile, "rb")) == NULL ) //     rb    只讀打開一個二進制文件,只允許讀數據

exit(0);

 

if( (out=fopen(outfile, "wb")) == NULL ) //      wb    只寫打開或建立一個二進制文件,只允許寫數據

exit(0);

 

c=fgetc(in); //fgetc(), getc() 從指定文件取得一個字符

while(!feof(in)) //文件狀態 feof() 若到文件末尾,函數值為真

{

fputc(c, out); //fputc(), putc() 把字符輸出到指定文件

c=fgetc(in);

}

 

fclose(in);

fclose(out);

 

return 0;

}

 

字符串讀寫函數:fgets 和 fputs

1、只能處理文本文件,不能處理二進制文件

2、返回值:

1. 輸入成功:返回值為0

2. 輸入失敗:返回EOF

 

例一:字符串寫入:

/*

2011092221 5225 

 

字符串讀寫函數:fgets 和 fputs

返回值:

1. 輸入成功:返回值為0

2. 輸入失敗:返回EOF

*/

 

#include <stdio.h>

#include <stdlib.h>

 

#define LEN 11;

 

void main()

{

FILE * fp;

 

char buffer[30], ch;

 

if( !(fp = fopen("qintangtao.txt", "at+")) ) //   at+” 讀寫打開一個文本文件,允許讀,或在文件末追加數據 文件指針指向了文件的結尾處

{

printf("\nCannot open file strike any key exit!");

exit(1);

}

 

printf("Please input a string:\n");

// scanf("%s", buffer);  // scanf();接收時 當遇到 空格、換行 就認為結束了

 

fgets(buffer, 30, stdin); //為什么不用scanf()? stdin又是什么?

 

fputs(buffer, fp);

 

rewind(fp); //重新定義文件內部指針指向文件的開頭處

 

ch = fgetc(fp);

 

while( ch != EOF )

{

putchar(ch); //輸出到屏幕上

ch = fgetc(fp);

}

 

printf("\n");

 

fclose(fp);

 

system("pause");

}

 

例二:字符串讀出

/*

2011092219 4044 

總結:

字符串讀寫函數:fgets 和 fputs

*/

 

#include <stdio.h>

#include <stdlib.h>

 

#define LEN 11;

 

void main()

{

FILE * fp;

 

char buffer[11];

 

if( !(fp = fopen("qintangtao.txt", "rt")) ) //是否能夠打開文件

{

printf("\nCannot open file strike any key exit!");

exit(1);

}

 

fgets(buffer, 11, fp); //fp中讀出11個字節存放在buffer

 

printf("%s\n", buffer);

 

fclose(fp);

 

system("pause");

}

 

 

/*

2011092514 0549 

功能:

輸入學生的信息存入student.txt文件中

*/

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

FILE * fp;

char student[50];

int i;

//scanf("%s", student); // 當輸入 1001 tom 92.3  時 只能把1001存入文件中 因為在遇到空格時 scanf就把它當成了結束的標記 所以要用下面的  gets(student); 來進行輸入

 

if( (fp=fopen("student.txt", "w"))==NULL )

exit(0);

 

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

{

gets(student);

fputs(student, fp); //整行的存入文件中

fputs("\n", fp); //換行

}

 

return 0;

}

 

/*

2011092514 1713 

功能:

student.txt文件中讀取數據並顯示在屏幕上

*/

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

FILE * fp;

 

char string[80];

 

if( (fp=fopen("student.txt", "r"))==NULL )

exit(0);

 

while( fgets(string, 80, fp)!=NULL )

{

//printf("%s", string); //也可以用下面的方法將內容顯示在屏幕上

fputs(string, stdout); //stdout函數是將內容顯示到屏幕上

}

 

 

return 0;

}

數據塊(二進制文件的)讀寫函數:fread 和 fwrite

函數調用:

fread(buffer, size, count, fp);

fwrite(buffer, size, count, fp);

 

參數說明:

buffer: 是一個指針

fread來說,它是讀入數據的存放地址

fwrite來說,是要輸出數據的地址(均指起始地址)

size: 要讀寫的字節數

count:要進行讀寫多少個size字節的數據項

fp:文件指針

 

例一:數據塊寫入:

/*

2011092223 0125 

總結:

函數調用:

fread(buffer, size, count, fp);

fwrite(buffer, size, count, fp);

 

參數說明:

buffer: 是一個指針

fread來說,它是讀入數據的存放地址

fwrite來說,是要輸出數據的地址(均指起始地址)

size: 要讀寫的字節數

Count:要進行讀寫多少個size字節的數據項

fp:文件的指針

*/

 

#include <stdio.h>

 

#define SIZE 4

 

struct student

{

char name[10];

int num;

int age;

char addr[15];

}stu[SIZE];

 

void save()

{

FILE *fp;

int i;

 

if( !(fp = fopen("student-list", "wb")) ) // wb” 只寫打開或建立一個二進制文件,只允許寫數據

{

printf("Cannot open the file!\n");

return;

}

 

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

{

if( fwrite(&stu[i], sizeof(struct student), 1, fp) !=1 )  //fwrite(buffer, size, count, fp);

{

printf("File write error!\n");

fclose(fp);

}

}

}

 

 

 

void main()

{

int i;

 

printf("Please input the student's name, num, age and address: \n");

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

{

scanf("%s %d %d %s", &stu[i].name, &stu[i].num, &stu[i].age, &stu[i].addr );

}

 

save();

}

 

例二:數據塊讀出:

/*

2011092223 0330 

總結:

函數調用:

fread(buffer, size, count, fp);

fwrite(buffer, size, count, fp);

 

參數說明:

buffer: 是一個指針

fread來說,它是讀入數據的存放地址

fwrite來說,是要輸出數據的地址(均指起始地址)

size: 要讀寫的字節數

Count:要進行讀寫多少個size字節的數據項

fp:文件的指針

*/

 

#include <stdio.h>

 

#define SIZE 4

 

struct student

{

char name[10];

int num;

int age;

char addr[15];

}stu[SIZE];

 

//函數聲明

void load();

 

void main()

{

int i;

 

load();

printf("      name     num    age      adress\n\n");

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

{

printf("%10s %5d %5d %10s \n", &stu[i].name, &stu[i].num, &stu[i].age, &stu[i].addr );

}

 

 

}

 

void load()

{

FILE *fp;

int i;

 

if( !(fp = fopen("student-list", "r")) )

{

printf("Cannot open the file!\n");

return;

}

 

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

{

fread(&stu[i], sizeof(struct student), 1, fp); //fread(buffer, size, count, fp);

}

 

fclose(fp);

};

 

 

/*

2011092520 1022 

功能:

據塊(二進制文件的)讀寫函數:fread 和 fwrite

函數調用:

fread(buffer, size, count, fp);

fwrite(buffer, size, count, fp);

 

參數說明:

buffer: 是一個指針

fread來說,它是讀入數據的存放地址

fwrite來說,是要輸出數據的地址(均指起始地址)

size: 要讀寫的字節數

count:要進行讀寫多少個size字節的數據項

fp:文件指針

*/

#include <stdio.h>

#include <stdlib.h>

 

struct student

{

long num;

char name[10];

int age;

} s[4];

 

int main(void)

{

FILE * fp;

int i;

 

if( (fp=fopen("student.dat", "w+")) == NULL ) //  w+”讀寫打開或建立一個文本文件,允許讀寫

exit(0);

 

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

{

printf("%d個學生:\n", i+1);

printf("學號:");

scanf("%ld", &s[i].num);

printf("姓名:");

scanf("%s", s[i].name);

printf("年齡:");

scanf("%d", &s[i].age);

//fwrite(&s, sizeof(struct student), 1, fp); // fwrite(buffer, size, count, fp);

}

 

fwrite(s, sizeof(struct student), 4, fp);

 

rewind(fp); //文件內部指針指向第一個字節

 

// for(j=0; j<4; j++)

// {

// fread(&s, sizeof(struct student), 1, fp); // fread(buffer, size, count, fp);

// printf("%ld %s %d\n", s.num, s.name, s.age);

// }

 

fread(s, sizeof(struct student), 4, fp); 

 

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

{

printf("%ld %s %d\n", s[i].num, s[i].name, s[i].age);

}

 

fclose(fp);

 

return 0;

}

 

 

格式化讀寫函數: fscanf fprinf 

函數調用:

fprintf(文件指針, 格式字符串, 輸出表列);

fscanf(文件指針, 格式字符串, 輸入表列);

 

函數功能:

從磁盤文件中按格式讀入或輸出字符。

例如:

fprintf(fp, "%d %6.2f", i, t);

fscanf(fp, "%d %f", &i, &t);

 

 

/*

2011092514 5607 

函數調用:

fprintf(文件指針, 格式字符串, 輸出表列);

fscanf(文件指針, 格式字符串, 輸入表列);

 

函數功能:

從磁盤文件中按格式讀入或輸出字符。

*/

#include <stdio.h>

 

FILE *stream;

 

void main( void )

{

   long l;

   float fp;

   char s[81];

   char c;

 

   stream = fopen( "fscanf.out", "w+" ); //   w+   讀寫打開或建立一個文本文件,允許讀寫

   if( stream == NULL )

      printf( "The file fscanf.out was not opened\n" );

   else

   {

      fprintf( stream, "%s %ld %f%c", "a-string",  //為什么%f%c之間沒有空格 fscanf( stream, "%c", &c );  才能把'x'讀取出來?

               65000, 3.14159, 'x' );

 

      /* Set pointer to beginning of file: */

      fseek( stream, 0L, SEEK_SET );//  改變文件的位置指針  fseek(文件類型指針, 位移量, 起始點); 

/*

文件開頭             SEEK_SET  0

文件當前位置      SEEK_CUR  1

文件末尾            SEEK_END 2

*/

 

      /* Read data back from file: */

      fscanf( stream, "%s", s );    //按照什么順序輸入就按照什么順序輸出

      fscanf( stream, "%ld", &l );

      fscanf( stream, "%f", &fp );

      fscanf( stream, "%c", &c );  

 

      /* Output data read: */

      printf( "%s\n", s );

      printf( "%ld\n", l );

      printf( "%f\n", fp );

      printf( "%c\n", c );

 

      fclose( stream );

   }

}

 

 

/*

2011092514 4007 

功能:

函數調用:

fprintf(文件指針, 格式字符串, 輸出表列);

函數功能:

從磁盤文件中按格式輸出字符。

*/

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

int i = 10;

double fp = 1.5;

char s[]="this is a string!";

char c = '\n';

 

FILE * stream;

 

stream = fopen("fprintf.out", "w");

fprintf(stream, "%s%c", s, c);

fprintf(stream, "%d\n", i);

fprintf(stream, "%f\n", fp);

fclose(stream);

system("type fprintf.out"); //此函數是將fprintf.out文件中的內容顯示在屏幕上

 

return 0;

}

 

/*

2011092514 4007 

功能:

格式化輸入學生信息-fprintf.c

 

函數調用:

fprintf(文件指針, 格式字符串, 輸出表列);

函數功能:

從磁盤文件中按格式輸出字符。

*/

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

FILE * fp;

long num;

char name[10];

int age;

int i;

 

if( (fp=fopen("stuendt.txt", "w")) == NULL )

exit(0);

 

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

{

printf("%d個學生:\n", i+1);

printf("學號:");

scanf("%ld", &num);

 

printf("姓名:");

scanf("%s", name);

 

printf("年齡:");

scanf("%d", &age);

 

fprintf(fp, "學號:%ld 姓名:%9s 年齡: %d\n", num, name, age);

}

 

 

fclose(fp);

 

return 0;

}

 

/*

2011092515 3736 

功能:

格式化輸出學生信息-fscanf.c

 

函數調用:

fscanf(文件指針, 格式字符串, 輸入表列);

 

函數功能:

從磁盤文件中按格式字符。

*/

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

FILE * fp;

long num;

char name[10];

int age;

 

if( (fp=fopen("stuendt.txt", "r")) == NULL )

exit(0);

 

while( !feof(fp))

{

// fprintf(fp, "學號:%ld 姓名:%9s 年齡: %d\n", num, name, age);

fscanf(fp, "學號:%ld 姓名:%9s 年齡: %d\n", &num, name, &age); //一定要按照 fprintf(fp, "學號:%ld 姓名:%9s 年齡: %d\n", num, name, age); 中存入的格式來讀取

printf("%ld %s %d\n", num, name, age);

}

 

 

fclose(fp);

 

return 0;

}

 

順序讀寫和隨機讀寫

順序讀寫:

位置指針按字節位置順序移動

 

隨機讀寫:fseek函數

讀寫完上一個字符(字節)后,並不一定要讀寫其后續的字符(字節),而可以讀寫文件中任意位置上多需要的字符(字節)

 

函數功能:

改變文件的位置指針

 

函數的調用形式:

fseek(文件類型指針, 位移量, 起始點);

 

參數:

起始點:

文件開頭            SEEK_SET  0

文件當前位置      SEEK_CUR  1

文件末尾           SEEK_END 2

 

例如:

fseek(fp, 100L, 0);

將位置指針移到離文件頭100個字節處

 

fseek(fp,50L, 1);

將位置指針移到離當前位置50個字節處

 

fseek(fp,10L, 2);

將位置指針從文件末尾處向后退10個字節

 

例子:

/*

2011092223 3853 

總結:

順序讀寫和隨機讀寫

順序讀寫:

位置指針按字節位置順序移動

 

隨機讀寫:fseek函數

讀寫完上一個字符(字節)后,並不一定要讀寫其后續的字符(字節),而可以讀寫文件中任意位置上多需要的字符(字節)

 

函數功能:

改變文件的位置指針

 

函數的調用形式:

fseek(文件類型指針, 位移量, 起始點);

 

參數:

起始點:

文件開頭            SEEK_SET  0

文件當前位置 SEEK_CUR  1

文件末尾 SEEK_END 2

 

例如:

fseek(fp, 100L, 0);

將位置指針移到離文件頭100個字節處

 

fseek(fp,50L, 1);

將位置指針移到離當前位置50個字節處

 

fseek(fp,10L, 2);

將位置指針從文件末尾處向后退10個字節

*/

#include <stdio.h>

#include <stdlib.h>

 

struct student

{

char name[10];

int num;

int age;

char addr[15];

}boy;

 

void main()

{

FILE *fp;

 

int i=1; //用於定位第 個結構

 

if( !(fp = fopen("student-list", "r")) )

{

printf("Cannot open the file!\n");

return;

}

 

rewind(fp); //設置文件內部指針指向文件頭

 

fseek(fp, i*sizeof(struct student), 0); // fseek(文件類型指針, 位移量, 起始點);

fread(&boy, sizeof(struct student), 1, fp); //數據塊讀寫函數:freed :   fread(buffer, size, count, fp);

//讀取一個學生的容量

/*

參數說明:

buffer: 是一個指針

fread來說,它是讀入數據的存放地址

fwrite來說,是要輸出數據的地址(均指起始地址)

size: 要讀寫的字節數

count:要進行讀寫多少個size字節的數據項

fp:文件指針

*/

printf("name number    age    addr\n");

printf("%s %5d   %7d      %s\n", boy.name, boy.age, boy.addr);

 

}

 

 

 

/*

2011092522 0236 

功能:

函數的調用形式:

fseek(文件類型指針, 位移量, 起始點);

 

參數:

起始點:

文件開頭            SEEK_SET  0

文件當前位置 SEEK_CUR  1

文件末尾 SEEK_END 2

 

例如:

fseek(fp, 100L, 0);

將位置指針移到離文件頭100個字節處

 

fseek(fp,50L, 1);

將位置指針移到離當前位置50個字節處

 

fseek(fp,10L, 2);

將位置指針從文件末尾處向后退10個字節

*/

#include <stdio.h>

#include <stdlib.h>

 

struct student

{

long num;

char name[10];

int age;

}s[10];

 

int main(void)

{

FILE *fp;

int i;

struct student stu;

 

if( (fp=(fopen("student.dat", "w+"))) == NULL )

exit(0);

 

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

{

printf("%d個學生:\n", i+1);

printf("學號:");

scanf("%ld", &s[i].num);

printf("姓名:");

scanf("%s", s[i].name);

printf("年齡:");

scanf("%d", &s[i].age);

}

 

fwrite(s, sizeof(struct student), 10, fp);

 

for(i=1; i<=9; i=i+2)

{

fseek(fp, (i-1)*sizeof(struct student), SEEK_SET);

fread(&stu, sizeof(struct student), 1, fp);

printf("%ld %s %d", stu.num, stu.name, stu.age);

}

 

fclose(fp);

return 0;

}

 

 

文件定位:

文件中有一個位置指針,指向當前讀寫的位置,如果順序讀寫一個文件,每次讀寫一個字符,則讀寫完一個字符后,該位置指針自動移動指向下一個字符位置。如果想改變這樣的規律,強制使位置指針指向其他指定的位置。可以用下面介紹的有關函數實現上述功能:

 

1、rewind函數

rewind函數的作用是使指針重新返回文件的開頭,此函數沒有返回值

 

/*

2011092520 2838 

功能:

stuendt.txt文件中的內容讀取出來顯示在屏幕上

然后將stuendt.txt中的內容復制到stuendt2.txt文件中

 

注意:

rewind函數的作用是使指針重新返回文件的開頭,此函數沒有返回值

*/

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

FILE * fp;

FILE * f;

char c;

 

if( (fp=fopen("stuendt.txt", "r")) == NULL )

exit(0);

 

c=fgetc(fp);

while(!feof(fp) )

{

// printf("%c", c);

fputc(c, stdout); //以上兩種方式都可以將stuendt.txt中的內容打印到屏幕上  stdout函數就是將內容顯示在屏幕上

c=fgetc(fp); 

}

 

if( (f=fopen("stuendt2.txt", "w")) == NULL )

exit(0);

 

rewind(fp); // rewind函數的作用是使指針重新返回文件的開頭,此函數沒有返回值

 

while(!feof(fp) )

{

fputc(c, f); //當向stuendt2.txt中寫入一個字符后文件內部指針就會自動指向后一個字節

c=fgetc(fp); 

}

 

fclose(fp);

fclose(f);

 

return 0;

}

 

 

 

ftell函數:

long ftell( FILE *stream );

函數作用:

得到流式文件中的當前位置,用相對於文件開頭的位移量來表示。

 

返回值:

返回當前位置,出錯時返回-1L

 

應用舉例:

i = ftell(fp);

if( i == -1L)

printf("error\n");

 

 

/*

2011092619 1434 

功能:

函數格式:

long ftell( FILE *stream );

函數作用:

得到流式文件中的當前位置,用相對於文件開頭的位移量來表示。

 

返回值:

返回當前位置,出錯時返回-1L

 

*/

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

FILE * stream;

long position;

char list[100];

if( (stream = fopen("我把的后綴換成zip.gif", "rb")) != NULL )

{

fread(list, sizeof(char), 100, stream);  // fread(buffer, size, count, fp); 從“我把的后綴換成zip.gif”文件中讀取100個字節放到list中 此時文件內部指針指向的位置由 long ftell( FILE *stream ); 來判斷

position = ftell(stream);  //long ftell( FILE *stream );    得到流式文件中的當前位置,用相對於文件開頭的位移量來表示。

printf("Position after trying to read 100 bytes: %ld\n", position);

fclose(stream);

}

 

return 0;

}

 

出錯的檢測

ferror函數

 

調用形式:

ferror(fp);

 

返回值:

返回0,表示未出錯;返回非0,表示出錯

 

注意:

在調用一個輸入輸出函數后立即檢查ferror函數的值,否則信息會丟失。在執行fopen函數時,ferror函數的初始值自動設置為0

 

/*

2011092619 3631 

  功能:

調用形式:

ferror(fp);

 

返回值:

返回0,表示未出錯;返回非0,表示出錯

 

注意:

在調用一個輸入輸出函數后立即檢查ferror函數的值,否則信息會丟失。在執行fopen函數時,ferror函數的初始值自動設置為0

 

 

注意:

1、在讀取文件時可能返回的字節數比實際文件的字節要小也可能相等

2、當要小的時候,是因為在文件中有換行符 在文件中換行符占兩個字節,但fread函數在讀取換行符的時候 會將換行符所占的兩個字節讀取成一個字節

3、當文件中只有一行數據,並且也不存在換行符,此時讀取的字節數就等於文件所占的字節

*/

 

#include <stdio.h>

#include <stdlib.h>

 

void main( void )

{

   int  count, total = 0;

   char buffer[100];

   FILE *stream;

 

   if( (stream = fopen( "qintangtao.txt", "r" )) == NULL )

      exit( 1 );

 

   /* Cycle until end of file reached: */

   while( !feof( stream ) )

   {

      /* Attempt to read in 10 bytes: */

      count = fread( buffer, sizeof( char ), 100, stream ); //fread函數可以讀取二進制文件 也可以讀文本文件

      if( ferror( stream ) )    //int ferror( FILE *stream );  返回0,表示未出錯;返回非0,表示出錯

  {

         perror( "Read error" );

         break;

      }

 

      /* Total up actual bytes read */

      total += count;

   }

   printf( "Number of bytes read = %d\n", total );

   fclose( stream );

}

 

 

 

clearerr函數

void clearerr( FILE *stream );

調用形式:

clearerr(fp);

 

函數作用:

使文件錯誤標志和文件結束標志設置為0

 

注意:

只要出現錯誤標志,就一直保留,直到對同一文件調用clearerr函數或rewind函數,或任何其他一個輸入輸出函數

 

/*

2011092620 1218 

功能:

void clearerr( FILE *stream );

 

調用形式:

clearerr(fp);

 

函數作用:

使文件錯誤標志和文件結束標志設置為0

 

注意:

只要出現錯誤標志,就一直保留,直到對同一文件調用clearerr函數或rewind函數,或任何其他一個輸入輸出函數

*/

#include <stdio.h>

 

void main( void )

{

   int c;

   /* Create an error by writing to standard input. */

   putc( 'c', stdin );

   if( ferror( stdin ) )

   {

      perror( "Write error" );

      clearerr( stdin ); //當出現錯誤時 用clearerr清除錯誤 

   }

  

 

   /* See if read causes an error. */

   printf( "Will input cause an error? " );

   c = getc( stdin );

   if( ferror( stdin ) )

   {

      perror( "Read error" );

      clearerr( stdin );

   }

  

}

 

 

 

以上函數都要求包含頭文件stdio.h

 

文件操作小結:

分類

函數名

功能

打開文件

fopen()

打開文件

關閉文件

fclose()

關閉文件

文件定位

fseek()

改變文件位置指針的位置

rewind()

使文件位置指針重新至於文件開頭

ftell()

返回未見位置指針的當前值

文件狀態

feof()

若到文件末尾,函數值為真

ferror()

若對文件操作出錯,函數值為真

clearerr()

使ferrorfeof函數值置零

文件讀寫

fgetc(), getc()

從指定文件取得一個字符

fputc(), putc()

把字符輸出到指定文件

fgets()

從指定文件讀取字符串

fputs()

把字符串輸出到指定文件

getw()

從指定文件讀取一個字(int型)

putw()

把一個字輸出到指定文件

fread()

從指定文件中讀取數據項

fwrite()

把數據項寫到指定文件中

fscanf()

從指定文件按格式輸入數據

fprintf()

按指定格式將數據寫到指定文件中

 

 

C\C++ 關於FILE結構定義說明

此結構VC定義於stdio.h頭文件中,

struct   _iobuf   


     char   *_ptr;            //文件輸入的下一個位置 

  int    _cnt;           //當前緩沖區的相對位置 
     char   *_base;          //指基礎位置(應該是文件的其始位置
     int    _flag;           //文件標志 
     int    _file;          //文件的有效性驗證 
     int    _charbuf;    //檢查緩沖區狀況,如果無緩沖區則不讀取 
     int    _bufsiz;      //文件的大小 
     char   *_tmpfname; //臨時文件名 
};
typedef   struct   _iobuf   FILE; 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM