user-0 提问: 判断文件是否读取完

回复:
user: user-0
content: 楼上详解！  学习ing

user: user-1
content: /* C语言测试文件尾的方法   首先先谈一般的，普通的 asc 文件 feof（FILE*）遇到文件尾时返回非0。但是feof测试文件尾，需要慎用，经常不小心会出现最后重复读取的错误。因为feof返回值是根据statement来决定的。当fgetc getc fgets fread返回失败的时候，才会修改statemnet，然后feof时候就会返回一个 非0值 说明已经到了文件尾了，该杀的时候了。我测试了windows和linux。其中windows每次都返回16， linux返回1，遇到文件尾的时候。总之返回非0就是遇到文件尾了，该杀了。。。   所以检测文件尾的时候最好要少用feof，暂时我也没发现用他的好方法。   判断尾巴的方法：   1. int ch = fgetc(fp);    if(ch == EOF)break; 2. fgets返回NULL 3. fread返回0   还有适当的配合文件指针来使用 文件指针从0开始到size-1（其中size尾文件大小），其中后面还有一个虚拟的指针size位置（文件尾） 打开文件时候，指针指向0。 ps：文件指针到底指向哪呢？实际上就是指向将要读取的一个字节的地址，以前读过的就不算了。所以刚开始打开文件的时候，文件指针的偏移量就是，指向文件的第一个字节，因为它还没有被读取。fseek(fp, 0, SEEK_END)就是跳过整个文件，指向文件尾巴。那个实际不存在的地方。如果此时getc的话就会返回-1出错了。但是此时调用feof是不会返回非0（遇到文件尾的），因为feof只是返回系统的一个statment的值，它是要getc gets fread等等系统调用修改的。所以再三强调feof不是一个好方法。   fseek(fp, 0, SEEK_END)后文件指针指向size,如果此时feof(fp)的话，是不会返回非0的，原因就是根本没有系统调用改变了那个statment的值。所以还是返回0，这样如果不做判断直接getc的话，就会获得一个-1到自己的程序中就会出错了。当然也因为这个-1（虽然你么有判断，但是系统害是该了你的statment）所以下一次调用feof的时候就会返回非0，然后才停止读取数据。但是已经错误了，所以大家要小心了啊  */

user: user-2
content: fscanf, fwscanf Read formatted data from a stream.  int fscanf( FILE *stream, const char *format [, argument ]... );  int fwscanf( FILE *stream, const wchar_t *format [, argument ]... );  Function Required Header Compatibility  fscanf <stdio.h> ANSI, Win 95, Win NT  fwscanf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. If an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF for fscanf or WEOF for fwscanf.  Parameters  stream  Pointer to FILE structure  format  Format-control string  argument  Optional arguments  Remarks  The fscanf function reads data from the current position of stream into the locations given by argument (if any). Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. format controls the interpretation of the input fields and has the same form and function as the format argument for scanf; see scanf for a description of format. If copying takes place between strings that overlap, the behavior is undefined.  fwscanf is a wide-character version of fscanf; the format argument to fwscanf is a wide-character string. These functions behave identically otherwise.  Generic-Text Routine Mappings  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _ftscanf fscanf fscanf fwscanf    For more information, see Format Specification Fields – scanf functions and wscanf Functions.  Example  /* FSCANF.C: This program writes formatted  * data to a file. It then uses fscanf to  * read the various data back from the file.  */  #include <stdio.h>  FILE *stream;  void main( void ) {    long l;    float fp;    char s[81];    char c;     stream = fopen( "fscanf.out", "w+" );    if( stream == NULL )       printf( "The file fscanf.out was not opened\n" );    else    {       fprintf( stream, "%s %ld %f%c", "a-string",                 65000, 3.14159, 'x' );        /* Set pointer to beginning of file: */       fseek( stream, 0L, SEEK_SET );        /* 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 );    } }   Output  a-string 65000 3.141590 x   Stream I/O Routines  See Also   _cscanf, fprintf, scanf, sscanf

user: user-2
content: int r; while(1)  ///<  这里怎么判断文件是否读取完毕  feof()函数实现不了      {          r=fscanf(fp, "%s %s %d", filte_info->file_name, filte_info->function_name, &filte_info->line_no);          if (r==3)          printf("%s\n%s\n%d\n", filte_info->file_name, filte_info->function_name, filte_info->line_no);          else if (r==EOF) break;          else printf("Format Error after %s %s %d!\n",filte_info->file_name, filte_info->function_name, filte_info->line_no);      }      fclose(fp);

user: user-0
content: 引用 2 楼 zhao4zhong1 的回复:请判断fscanf函数的返回值。 while(feof(fp)) 中 feof(fp)为0  进不了循环。

user: user-0
content: 额。已解决。   在while 之前加上 fseek(fp, 0, SEEK_SET);

user: user-2
content: 请判断fscanf函数的返回值。

user: user-3
content: if(fread(buffer, sizeof(buffer), 1, fp) < 1) {     if(feof())     {         printf("read to file tail\n");     }     else     {         do something;     } }