怪事,fread 1A的问题,
fread函数读到0X1A时
feof(p)==0
这是什么问题?
有没有什么解决办法?
问题点数:80、回复次数:7Top
1 楼pushser(捕食者)回复于 2003-08-02 00:19:35 得分 0
char buf[10];
p=fopen("data.dat","r");
while(!feof(p))
fread(buf,sizeof(char),10,p)
一次读25个字符到buf字符串中。。。
注意其中没有异常处理!Top
2 楼pengzhenwanli(紫气日盈)回复于 2003-08-02 01:41:06 得分 0
贴出源程序Top
3 楼StdAfx(任杰)回复于 2003-08-02 03:18:21 得分 0
你说的这种情况确实怪。我也不知道为什么。
/* 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>
#include <stdlib.h>
void main( void )
{
int count, total = 0;
char buffer[100];
FILE *stream;
if( (stream = fopen( "feof.c", "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 );
if( ferror( stream ) ) {
perror( "Read error" );
break;
}
/* Total up actual bytes read */
total += count;
}
printf( "Number of bytes read = %d\n", total );
fclose( stream );
}
Top
4 楼windleee(风子)回复于 2003-08-02 03:54:07 得分 0
to StdAfx(任杰):
你在打开的文件里有没有0X1A?
加一个试试Top
5 楼qqchen79(知秋一叶)回复于 2003-08-02 05:28:48 得分 80
文本文件是根据特殊字符EOF(0x1A)标志结束的,而不是文件长度。
fopen缺省以文本方式打开,如果需要以二进制打开:
fopen( "feof.c", "rb" );Top
6 楼StdAfx(任杰)回复于 2003-08-02 06:38:46 得分 0
是的 qqchen79(知秋一叶 [MS MVP]) 说得很对。Top
7 楼windleee(风子)回复于 2003-08-03 00:51:23 得分 0
fopen( "feof.c", "rb" );
好象读到1A也出错哦Top



