从文件中读取对象

时间:2011-06-30 22:03:03

标签: c++ file serialization object

我有这段代码:

#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 Information\n";
    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;
}

当我从文件中读取时,最后一个对象打印两次。我该怎么办?

3 个答案:

答案 0 :(得分:1)

尝试

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 Information\n";
        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;
}

答案 1 :(得分:1)

如果我在猜测,这是一个很好的猜测,那将是循环中的feof测试。它可能是在读取下一行并且未能获得完整记录后检测到feof。

你应该在while循环中读取并测试不适当的回复,或者至少在阅读后再次检查feof。

答案 2 :(得分:0)

首先你可以重写它,所以类有一个序列化/反序列化方法,它需要一个输出/输入流参数。此代码不可重用。您还想重写代码以执行输出std::ostream& operator<<(std::ostream& s, Student const& rhs)等。您的代码是一大堆 censored 。如果你重写它,那么主逻辑将是几行,每个花30秒检查它的人都可以显示出问题所在。