【问题标题】:read objects from file从文件中读取对象
【发布时间】:2011-09-26 08:44:45
【问题描述】:

我有这个代码:

#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;
}

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

【问题讨论】:

  • 这样的书写结构是不可移植的。它依赖于特定于主机的打包、浮点表示和各种丑陋的东西,并且在结构中包含漏洞。您应该以序列化、规范化的形式编写它。
  • @Seth:你是对的。我认为公平地说,这很可能是家庭作业,这对于示例来说是可以的
  • @sehe:在他们有机会在更严重的项目中犯这样的错误之前对他们进行培训。

标签: c++ file serialization object


【解决方案1】:

首先,您可以重写它,以便该类具有一个序列化/反序列化方法,该方法需要一个输出/输入流参数。此代码不可重用。您还想重写代码以进行输出std::ostream&amp; operator&lt;&lt;(std::ostream&amp; s, Student const&amp; rhs) 等。您的代码是一大堆审查。如果你重写它,主要逻辑将是几行代码,每个花费 30 秒检查它的人都可以指出问题出在哪里。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • @sehe:feof 函数也存在于 C++ 中,它不是 C 语言特有的。
    • @Thomas:是的,但请注意它对 std::istream 绝对没用
    【解决方案3】:

    试试

    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;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多