c中的文件问题

joy_91 2011-11-17 09:24:17
输出结果为read error!16
feof结果怎么会是16呢。。。。为什么会读入不了呢
其中lineinfo为结构体


void main()
{ FILE *fp;

int i=0;
if ((fp=fopen("F:\data.txt","rb"))==NULL)
{printf("cannot open file!");
exit(0);}

while(!feof(fp))

{
if(fread(&lineinfo[0],sizeof(struct airline),1,fp)!=1)
{ printf("read error!");
printf("%d",feof(fp));
exit(0);}
}
fclose(fp);}
...全文
131 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-11-17
  • 打赏
  • 举报
回复
推荐使用WinHex软件查看文件原始字节内容。
a707000646 2011-11-17
  • 打赏
  • 举报
回复
不好意思 我的代码多了个getchar(),记得回车一下
a707000646 2011-11-17
  • 打赏
  • 举报
回复
返回非零值 2楼的朋友的帖子我看过了,他那里说的解决方法就是让你改读写方式
测试下,帮助文档上面只说事非零值
joy_91 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 selooloo 的回复:]
你的文件是用fwrite写的吗
[/Quote]

fread
joy_91 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 a707000646 的回复:]
The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file
The feof routine (implemented both as a function and as a macro) determines whe……
[/Quote]

是不是说若没有到结尾就返回任意实数。。。。为什么是16呢
a707000646 2011-11-17
  • 打赏
  • 举报
回复

#include<stdio.h>
struct airline
{
int a;
int b;
};
int main()
{ FILE *fp;
struct airline lineinfo[4];
lineinfo[0].a = 1;
lineinfo[0].b = 2;
lineinfo[1].a = 3;
lineinfo[1].b = 4;
int i=2;
if((fp = fopen("lineinfo.out","wb")) == NULL)
{
printf("cannot open file!");
exit(0);
}
fwrite(&lineinfo[0],sizeof(struct airline),1,fp);
fwrite(&lineinfo[1],sizeof(struct airline),1,fp);
fclose(fp);
getchar();
if ((fp=fopen("lineinfo.out","rb+"))==NULL)
{printf("cannot open file!");
exit(0);}

while(!feof(fp))
{
fread(&lineinfo[i++],sizeof(struct airline),1,fp);
}
printf("%d %d\n",lineinfo[2].a,lineinfo[2].b);
printf("%d %d",lineinfo[3].a,lineinfo[3].b);
fclose(fp);
system("pause");
return 0;
}
joy_91 2011-11-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dizuo 的回复:]
http://www.cplusplus.com/forum/general/22522/
fp=fopen("F:\data.txt","r") 试试
[/Quote]

试过了,还是没有用,结果一样
我想会不会是因为fread无法访问,所以对feof产生影响?
fread又为什么就是无法读出呢
selooloo 2011-11-17
  • 打赏
  • 举报
回复
你的文件是用fwrite写的吗
a707000646 2011-11-17
  • 打赏
  • 举报
回复
The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file
The feof routine (implemented both as a function and as a macro) determines whether the end of stream has been reached. When end of file is reached, read operations return an end-of-file indicator until the stream is closed or until fsetpos, fseek, or clearerr is called against it.
feof例程(作为一个函数和宏实现)确定是否已到达流的末尾。当到达文件末尾,读操作返回一个文件结束的指标,直到流关闭或直到fsetpos,FSEEK,或clearerr发生响应。
ryfdizuo 2011-11-17
  • 打赏
  • 举报
回复
http://www.cplusplus.com/forum/general/22522/
fp=fopen("F:\data.txt","r") 试试
qq120848369 2011-11-17
  • 打赏
  • 举报
回复
还没fread就判断feof永远都是false.

69,382

社区成员

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

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