开发者社区 问答 正文

这个程序为什么输出的是25个呢?

#include 
#include 
#include
using namespace std;
struct node
{
int x,y;
};
int main(){
FILE* f1 = fopen("in.dat","w");
node a;
for(int i = 0;i < 10000;i++){
    a.x = rand();
    a.y = rand();
    fwrite(&a,sizeof(a),1,f1);
}

fclose(f1);

f1 = fopen("in.dat","r");

int i = 1;
while(!feof(f1)){
    fread(&a,sizeof(a),1,f1);
    printf("%d\t",i++); 
}
cout << endl;
}

展开
收起
a123456678 2016-03-23 11:06:10 1999 分享 版权
1 条回答
写回答
取消 提交回答
  • 是rand取随机数的问题,你把a.x和a.y赋值为1和2之类的数就好了。
    其实你看看即使你不改in.dat文件是80000字节大小,说明输出了10000次。但是只读了25次,应该是输入到文件里的内容有什么问题,让程序出错了。

    2019-07-17 19:10:34
    赞同 展开评论
问答地址: