【问题标题】:error in reading and writing in files [closed]文件读写错误[关闭]
【发布时间】:2014-11-05 08:54:12
【问题描述】:

这个程序可以工作,但会在开头打印垃圾值 (ch=2) 并打印两次相同的输出。 我在我的项目中使用它。它包含更多数据,我使用单个对象而不是对象数组。它不起作用。数组的每个对象都存储一组数据。

   #include<iostream>
   #include<fstream>
       using namespace std;
       class rw               //a class containing some data
       {
             public:
             int n;
             char a;
             };
       int main()

       {
           int i;
           rw r[2];           //an array of objects
           int ch;
           fstream f1;
           f1.open("file1.txt",ios::in|ios::out|ios::ate);
           cout<<"1-read,2-write";
           cin>>ch;
           f1.seekp(0,ios::beg);
           if(ch==1)//for saving
           {
                    r[0].n=1;
                    r[0].a='a';
                    f1.write((char*) &r[0],sizeof(r[0]));
                    r[1].n=2;
                    r[1].a='b';
                    f1.write((char*)&r[1],sizeof(r[1]));
                    }
           if(ch==2)//for reading
           {
                    f1.seekg(0,ios::beg);
           while(!f1.eof())
           {
                           i=0;
                           cout<<r[i].n<<r[i].a;
                           cout<<"\n";
                           f1.read((char*)&r[i],sizeof(r[i]));
                           i++;
                           }
                           }
                           system("pause");
                           return 0;
                           }

【问题讨论】:

  • didn't work 是什么意思,请您详细说明一下?程序会崩溃吗?它只是不写出文件吗?

标签: c++ file class object


【解决方案1】:

更改以下代码

cout<<r[i].n<<r[i].a;
cout<<"\n";
f1.read((char*)&r[i],sizeof(r[i]));

f1.read((char*)&r[i],sizeof(r[i])); // Moved before printing
cout<<r[i].n<<r[i].a;
cout<<"\n";

您正在输出数据然后从文件中读取,而不是您应该读取然后打印。在阅读之前打印是在第一次循环运行时获得垃圾的原因。我认为您移动了下面的读数以避免重复打印最后一条记录。继续阅读以了解如何避免重复阅读。

您应该避免使用while (!f1.eof()),因为它会打印两次最后的数据。 更好用while(f1.read((char*)&amp;r[i],sizeof(r[i])));

为 2 个输入展开循环的 eof 版本

while(!eof) // true
read first data
print first data
while(!eof) // true
read second data  // read all, but was succesful and eof not set
print second data
while(!eof)  // true
Try to read // Could not read, eof set
print data // Reprinting second data
while(!eof) // false

【讨论】:

  • 你已经在阅读while,所以你不应该在循环中再次阅读。
  • 我删除了它..现在它崩溃了
  • 循环如何终止?
  • 它崩溃是因为 r 数组的边不足。使其更大或不增加 i。当您无法从文件中读取更多数据时,循环终止。
【解决方案2】:

不要使用while (!f1.eof()),它不会像你期望的那样工作,因为eofbit 标志直到你尝试从文件之外读取时才会设置。相反,例如while (f1.read(...)).

还要小心像这样没有边界检查的循环。如果文件不正确,您可能会写出数组r 的范围。

【讨论】:

  • 这确实应该是一条评论,因为它没有回答问题(参见上面@Mohit 的回答)。
  • @cybermonkey 这不是一个 完整 的答案,因为它回答了 OP 的两个问题,因为 Mohits 的答案处理了另一种情况,但不是这个(在我写这篇文章的时候回答)。
  • 这仍然不是答案,据我所知,OP只问了一个问题(实际上没有问任何问题!)。
  • @cybermonkey Mohits 回答,在相当广泛的编辑之前,回答了关于垃圾数据的问题。我的回答回答了打印两次的问题。这两个问题都在问题主体的第一句话中被“询问”。仅仅因为其他人编辑他们的答案以包含我写的内容,我写下我的答案之后,并不意味着我的答案不那么有效。
猜你喜欢
  • 2013-05-23
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多