【问题标题】:Writing file data into a class? C++将文件数据写入类? C++
【发布时间】:2013-05-06 00:43:34
【问题描述】:

我有一个如下所示的课程:

class Test
{
public:
    Test() {}
    ~Test() {}

    //I kept these public for simplicity's sake at this point (stead of setters).
    int first_field;
    int second_field;
    int third_field;
    string name;
};

我的 .txt 文件如下所示:

1  2323   88   Test Name A1
2  23432  70   Test Name A2
3  123    67   Test Name B1
4  2332   100  Test Name B2
5  2141   98   Test Name C1
7  2133   12   Test Name C2

我希望能够将文件中的每一行读入一个向量,所以我当前的代码如下所示:

#include "Test.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    ifstream file;
    vector<Test> test_vec;

    file.open("test.txt");
    if(file.fail())
    {
        cout << "ERROR: Cannot open the file." << endl;
        exit(0);
    }

    while(!file.eof())
    {
        string input;
        if(file.peek() != '\n' && !file.eof())
        {
            Test test;

            file >> test.first_field >> test.second_field >> test.third_field;
            getline(file, input);
            test.name = input;

            test_vec.push_back(test);
        }
    }

    return 0;
}

所以,我被困在要读取该数据的部分...我尝试了输入流运算符,但它什么也没做;其他选项给我错误。如果可能的话,我还想保留格式。我以后想做的是能够按类中的不同数据字段对该向量进行排序。

有什么想法吗?

编辑:问题已解决,代码已被编辑以反映它。谢谢大家的帮助。 :)

【问题讨论】:

    标签: c++ file class vector


    【解决方案1】:

    在读取文件并将其解析为类的对象之前应该解决的一个问题是:

      Test test;
      test.push_back(test);
    

    您的局部变量testTest 类的对象,它隐藏了您的向量:

    vector<Test> test;
    

    当你这样做时:

    test.push_back(test);
    

    你会遇到麻烦。尝试将您的对象命名为不同的名称。

    现在你可以考虑如何处理每一行,你可能会做如下:

    ifstream file;
    file.open("test.txt");
    int first_field, second_field, third_field;
    string testName;
    while (!file.eof()) {
       file >> first_field >> second_field >> third_field;
       getline(file, testName);
       cout << first_field <<" " <<  second_field << " " << third_field;
       cout << endl << testName <<endl;
    
        //you should set your constructor of Test to accept those four parameters
        //such that you can create objects of Test and push them to vector
    }
    

    【讨论】:

    • @Noobacode 我更新了帖子以处理文件。请看一下
    • 啊,这就是我搞砸的,我没有专门输入字符串来获取整个名称,包括任何空白字符。非常感谢您,先生,它完全符合我的要求。感谢您的宝贵时间和帮助。
    【解决方案2】:

    tacp 的回答很好,但这里有两件事我认为 C++/STL 更惯用:

    使用operator &gt;&gt;

    istream & operator>>(istream &in, Test &t)
    {
      in >> t.first_field >> t.second_field >> t.third_field;
      getline(in, t.name);
      return in;
    }
    

    此时读取文件看起来像这样

    ifstream file("test.txt");
    vector<Test> tests;
    Test test;
    while(file >> test) {
      tests.push_back(test);
    }
    

    使用streamiterator/迭代器构造函数

    要使其成为超级 STL 惯用语,请使用 istream_iterator(不要忘记 #include&lt;iterator&gt;),并从迭代器构造 vector

    ifstream file("test.txt");
    istream_iterator<Test> file_it(file), eos;
    vector<Test> tests(file_it, eos);
    

    PS:我赞成 Noobacode 的简单示例代码,并带有示例输入。也就是说,如果您保留原始代码,我个人会更愿意,因为这样可以让其他人在他们偶然发现这个问题时更容易理解您的原始问题。

    【讨论】:

    • 我同意你关于通过使用运算符重载来优化事物的观点。这绝对是我会实施以改进程序的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多