正文

feof2005-08-14 01:12:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/billgacsli/3780.html

分享到:

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

阅读(3667) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册