MSDN 2001 ------------------------ feof Tests for end-of-file on a stream. int feof( FILE *stream ); The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return. Example /* FEOF.C: This program uses feof to indicate when * it reaches the end of the file FEOF.C. It also * checks for errors with ferror. */ ********************************************************* #include<stdio.h> struct number { int num; }a[10],b[10]; int main(void) { int i; FILE *fp; fp=fopen("d:\\abc.txt","w+"); for(i=0;i<10;i++) a[i].num=i; for(i=0;i<10;i++) fwrite(&a[i],sizeof(struct number),1,fp); rewind(fp); i=0; while(!feof(fp)) fread(&b[i++],sizeof(struct number),1,fp); for(i=0;i<10;i++) printf("%d ",b[i].num); getch(); return 0; } ------------------------------------------- #include<stdio.h> int main() { FILE *fp; float a; int i; fp=fopen("F1.txt","w"); if( fp==NULL ){ printf("can't open the file"); return 0; } for( i=0;i<5;i++){ scanf("%f",&a); //我在这输入1空格2空格3空格4空格5<回车> fprintf(fp,"%2.1f\n", a); } fclose(fp); fp=fopen("F1.txt","r"); if( fp==NULL ) { printf("can't open file"); return 0; }//程序到这都没错,到下面时,变成死循环了, //而且屏幕上面一直打印5.0,问题是为什么是死循环啊,高手救救我 while(true){ fscanf(fp,"%2.1f",&a); printf("%2.1f\n" ,a); if(!feof(fp))break; } printf("\n"); fclose(fp); getchar(); return 0; } -----------------修改版------------------ #include<stdio.h> #include<stdlib.h> main() { FILE *fp; float a;int i; fp=fopen("F1.txt","w"); if(fp==NULL){ printf("can't open the file"); exit(0); } for( i=0;i<5;i++){ scanf("%f2.1",&a);//注意写入格式 fprintf(fp,"%2.1f\n" ,a); } fclose(fp); fp=fopen("F1.txt","r"); if(fp==NULL){ printf("can't open file"); exit(0); } while(fscanf(fp,"%f2.1\n",&a) != EOF ){// 写入写出格式要一致 system("pause"); printf("%2.1f\n" ,a); } printf("\n"); fclose(fp); getch(); }

评论