C语言feof问题?

desertion76 2017-03-15 09:43:07
题主大一C语言小白。。。今天在写一个从文件中读取链表中的程序的时候发现运行之后总是多出一组乱码数据 上网查过后后发现是feof的问题 只有fread结尾一次才会返回非零值 也就是说读到结尾之后还会再读一次 但是没想到怎么改呀。。。网上的都是fgetc的改法 我這个是要存到链表里呀 改的方法不一样呀 求大神帮帮忙指点一下怎么搞/(ㄒoㄒ)/~~ list传的是已经声明的链表头 内两个printf函数是之前找错用的 请大噶帮帮忙/(ㄒoㄒ)/~~
void put_ent_list(node1 *list,FILE *fq)
{
while(1)
{
fread(list,sizeof(node1),1,fq);
printf("aaa\n");
if(feof(fq))
break;
list->next=(node1 *)malloc(sizeof(node1));
printf("bbb\n");
list=list->next;
}
list=NULL;
fclose(fq);
}
...全文
168 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
desertion76 2017-03-17
  • 打赏
  • 举报
回复
引用 8 楼 赵4老师的回复:
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fseek,ftell,fread,fwrite,fgetc,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
谢谢!
赵4老师 2017-03-16
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fseek,ftell,fread,fwrite,fgetc,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
赵4老师 2017-03-16
  • 打赏
  • 举报
回复
为什么不判断fread的返回值呢? fread Reads data from a stream. size_t fread( void *buffer, size_t size, size_t count, FILE *stream ); Function Required Header Compatibility fread <stdio.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 fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged. Parameters buffer Storage location for data size Item size in bytes count Maximum number of items to be read stream Pointer to FILE structure Remarks The fread function reads up to count items of size bytes from the input stream and stores them in buffer. The file pointer associated with stream (if there is one) is increased by the number of bytes actually read. If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters. The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs. The value of a partially read item cannot be determined. Example /* FREAD.C: This program opens a file named FREAD.OUT and * writes 25 characters to the file. It then tries to open * FREAD.OUT and read in 25 characters. If the attempt succeeds, * the program displays the number of actual items read. */ #include <stdio.h> void main( void ) { FILE *stream; char list[30]; int i, numread, numwritten; /* Open file in text mode: */ if( (stream = fopen( "fread.out", "w+t" )) != NULL ) { for ( i = 0; i < 25; i++ ) list[i] = (char)('z' - i); /* Write 25 characters to stream */ numwritten = fwrite( list, sizeof( char ), 25, stream ); printf( "Wrote %d items\n", numwritten ); fclose( stream ); } else printf( "Problem opening the file\n" ); if( (stream = fopen( "fread.out", "r+t" )) != NULL ) { /* Attempt to read in 25 characters */ numread = fread( list, sizeof( char ), 25, stream ); printf( "Number of items read = %d\n", numread ); printf( "Contents of buffer = %.25s\n", list ); fclose( stream ); } else printf( "File could not be opened\n" ); } Output Wrote 25 items Number of items read = 25 Contents of buffer = zyxwvutsrqponmlkjihgfedcb Stream I/O Routines See Also fwrite, _read
desertion76 2017-03-15
  • 打赏
  • 举报
回复
引用 4 楼 desertion76的回复:
[quote=引用 3 楼 自信男孩的回复:][quote=引用 2 楼 desertion76 的回复:] [quote=引用 1 楼 自信男孩的回复:]我的建议是用feof 判断的同时也要判断fread的返回值,如果读到EOF,feof还没有为0,那么fread读到的数据长度也不会正确。这样就可以认为其读到的文件末尾
额额 不是读到EOF的时候feof不返回非零 是因为要读一次eof 所以多malloc了一块内存 在别的地方把链表打出来的时候就多了一块乱码。。。 之前试了在if判断之后把list free掉 但是好像还是有乱码。。。[/quote] 最后一个node,如果fread返回的值和预想的值小,那么就不要把该node加到链表里了。 如果还有乱码,应该是文件问题,跟读应该没关系了。[/quote] 刚刚洗了个脸。。。也不算是乱码吧 容我来个图[/quote] 额额 刚刚的图被吞了
desertion76 2017-03-15
  • 打赏
  • 举报
回复
存的时候是有4组数据 读的时候打出来了五行aaa
desertion76 2017-03-15
  • 打赏
  • 举报
回复
引用 3 楼 自信男孩的回复:
[quote=引用 2 楼 desertion76 的回复:] [quote=引用 1 楼 自信男孩的回复:]我的建议是用feof 判断的同时也要判断fread的返回值,如果读到EOF,feof还没有为0,那么fread读到的数据长度也不会正确。这样就可以认为其读到的文件末尾
额额 不是读到EOF的时候feof不返回非零 是因为要读一次eof 所以多malloc了一块内存 在别的地方把链表打出来的时候就多了一块乱码。。。 之前试了在if判断之后把list free掉 但是好像还是有乱码。。。[/quote] 最后一个node,如果fread返回的值和预想的值小,那么就不要把该node加到链表里了。 如果还有乱码,应该是文件问题,跟读应该没关系了。[/quote] 刚刚洗了个脸。。。也不算是乱码吧 容我来个图
自信男孩 2017-03-15
  • 打赏
  • 举报
回复
引用 2 楼 desertion76 的回复:
[quote=引用 1 楼 自信男孩的回复:]我的建议是用feof 判断的同时也要判断fread的返回值,如果读到EOF,feof还没有为0,那么fread读到的数据长度也不会正确。这样就可以认为其读到的文件末尾
额额 不是读到EOF的时候feof不返回非零 是因为要读一次eof 所以多malloc了一块内存 在别的地方把链表打出来的时候就多了一块乱码。。。 之前试了在if判断之后把list free掉 但是好像还是有乱码。。。[/quote] 最后一个node,如果fread返回的值和预想的值小,那么就不要把该node加到链表里了。 如果还有乱码,应该是文件问题,跟读应该没关系了。
desertion76 2017-03-15
  • 打赏
  • 举报
回复
引用 1 楼 自信男孩的回复:
我的建议是用feof 判断的同时也要判断fread的返回值,如果读到EOF,feof还没有为0,那么fread读到的数据长度也不会正确。这样就可以认为其读到的文件末尾
额额 不是读到EOF的时候feof不返回非零 是因为要读一次eof 所以多malloc了一块内存 在别的地方把链表打出来的时候就多了一块乱码。。。 之前试了在if判断之后把list free掉 但是好像还是有乱码。。。
自信男孩 2017-03-15
  • 打赏
  • 举报
回复
我的建议是用feof 判断的同时也要判断fread的返回值,如果读到EOF,feof还没有为0,那么fread读到的数据长度也不会正确。这样就可以认为其读到的文件末尾

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧