从文件中读取对象
read objects from file
我有这个代码:
#include <fstream>
#include <iostream>
using namespace std;
class Student
{
public:
char FullName[40];
char CompleteAddress[120];
char Gender;
double Age;
bool LivesInASingleParentHome;
};
int main()
{
Student one;
strcpy(one.FullName, "Ernestine Waller");
strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
one.Gender = 'F';
one.Age = 16.50;
one.LivesInASingleParentHome = true;
ofstream ofs("fifthgrade.ros", ios::binary);
ofs.write((char *)&one, sizeof(one));
Student three;
strcpy(three.FullName, "three Waller");
strcpy(three.CompleteAddress, "three 824 Larson Drv, Silver Spring, MD 20910");
three.Gender = 'M';
three.Age = 17;
three.LivesInASingleParentHome = true;
//ofstream ofs("fifthgrade.ros", ios::binary);
ofs.write((char *)&three, sizeof(three));*/
Student two;
ifstream ifs("fifthgrade.ros", ios::binary);
while(!(ifs.eof())){
ifs.read((char *)&two, sizeof(two));
cout << "Student Informationn";
cout << "Student Name: " << two.FullName << endl;
cout << "Address: " << two.CompleteAddress << endl;
if( two.Gender == 'f' || two.Gender == 'F' )
cout << "Gender: Female" << endl;
else if( two.Gender == 'm' || two.Gender == 'M' )
cout << "Gender: Male" << endl;
else
cout << "Gender: Unknown" << endl;
cout << "Age: " << two.Age << endl;
if( two.LivesInASingleParentHome == true )
cout << "Lives in a single parent home" << endl;
else
cout << "Doesn't live in a single parent home" << endl;
cout << "n";
}
return 0;
}
当我从文件中读取时,最后一个对象打印两次。我该怎么办?
尝试
while(ifs.read((char *)&two, sizeof(two)))
而不是
while(!(ifs.eof()))
也可以尝试格式化您的代码:)
#include <fstream>
#include <iostream>
using namespace std;
class Student
{
public:
char FullName[40];
char CompleteAddress[120];
char Gender;
double Age;
bool LivesInASingleParentHome;
};
int main()
{
/*Student one;
strcpy(one.FullName, "Ernestine Waller");
strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
one.Gender = 'F';
one.Age = 16.50;
one.LivesInASingleParentHome = true;
ofstream ofs("fifthgrade.ros", ios::binary);
ofs.write((char *)&one, sizeof(one));
Student three;
strcpy(three.FullName, "three Waller");
strcpy(three.CompleteAddress, "three 824 Larson Drv, Silver Spring, MD 20910");
three.Gender = 'M';
three.Age = 17;
three.LivesInASingleParentHome = true;
//ofstream ofs("fifthgrade.ros", ios::binary);
ofs.write((char *)&three, sizeof(three));*/
Student two;
ifstream ifs("fifthgrade.ros", ios::binary);
while(ifs.read((char *)&two, sizeof(two)))
{
cout << "Student Informationn";
cout << "Student Name: " << two.FullName << endl;
cout << "Address: " << two.CompleteAddress << endl;
if( two.Gender == 'f' || two.Gender == 'F' )
cout << "Gender: Female" << endl;
else if( two.Gender == 'm' || two.Gender == 'M' )
cout << "Gender: Male" << endl;
else
cout << "Gender: Unknown" << endl;
cout << "Age: " << two.Age << endl;
if( two.LivesInASingleParentHome == true )
cout << "Lives in a single parent home" << endl;
else
cout << "Doesn't live in a single parent home" << endl;
cout << "n";
}
return 0;
}
如果我在猜测,而且这是一个很好的猜测,那么它将是循环中的feof测试。它可能在读取下一行并未能获得完整记录后检测到feof。
你应该在while循环中读取,并测试是否有不恰当的回复,或者至少在读取后再次检查feof。
对于初学者来说,您可以重写它,这样类就有了一个序列化/反序列化方法,该方法需要一个输出/输入流参数。此代码不可重复使用。您还需要重写用于输出std::ostream& operator<<(std::ostream& s, Student const& rhs)
等的代码。您的代码是一大堆经过审查的。如果你重写它,主要逻辑将是几行,每个花30秒检查它的人都可以显示问题所在
相关文章:
- 如何在C++中从文件中读取/写入多个对象
- 使用对象文件读取三角形数据网格
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 我可以读取静态对象中的文件.txt吗?C++
- 从对象C++中的文件读取内容时出现分段错误
- 如何从文件中读取指向对象的指针?
- 如何在 C++ 中使用提升属性树从 JSON 文件中读取对象数组
- 从一个文件中读取多个序列化对象
- 读取互斥对象范围之外的volatile变量,而不是std::atomic
- 用rapidjson读取子对象向量
- 使用 OpengGLES2(角度)从帧缓冲区对象读取
- C++两次从文件保存对象读取多重继承
- 通过 ifstream 对象读取C++中的文件
- 将C 对象读取到随机访问文件中
- 使用 C++ 将对象读取和写入二进制文件
- C++在尝试编写对象读取器时,在尝试从一个向量获取数据时出错
- Boost.python 将对象拉取到本地范围内以进行读取访问
- 为什么这个ifstream无法将平面对象读取到内存中
- 如何在C++中从istream对象读取时检测空行
- C++使用fstream对象读取和写入文本文件时出现问题