代码之家  ›  专栏  ›  技术社区  ›  bethm

c++basic ifstream seekg()未加载

  •  0
  • bethm  · 技术社区  · 7 年前

    我已经花了两天多的时间从一个文件导入到我的对象中,阅读了这本书,搜索了互联网,基本上尝试了我能想到的一切。。所以这里是最后一条路(当然我永远不会放弃哈哈)&燃气轮机&燃气轮机&燃气轮机;我有一个用于软件对象的类ItemClass,我将其放置在一个向量中,最终将其更新并写回文件。我的完美结果是将文件的内容直接读取到软件对象的向量中,但现在我只想正确获取文件的内容。因此,我正在读取的文件是:

    Adobe Photoshop
    CS5
    5 21 580
    诺顿电脑优化大师
    不适用
    1 10 50
    诺顿系统工作
    2009
    3 6 50
    Visual Studio专业版
    2010
    4 19 700
    Microsoft Office
    2010
    6 27 150

    以下是未按预期运行的代码:

    int main() {
    
    //declare  new classes to play with file input/output
    ItemClass pshop;
    ItemClass nU;
    ItemClass sW;
    ItemClass vsP;
    
    //declare a vector to hold the objects for later sorting and placing into a tree structure
    vector<ItemClass> classVector;
    ifstream file;
    file.open("software.txt");
    
    if (file.fail()) {
        cout << "Error reading from file." << endl;
    }
            //edited code
            std::string kqpline;
            getline(file, kqpline);
            std::istringstream kqpstream(kqpline);
            kqpstream >> k >> q >> p;
    
            pshop.setItem(k, n, v, q, p);
    
    
           getline(file, n);
           getline(file, v);
           getline(file, kqpline);
           kqpstream >> k >> q >> p;
           nU.setItem(k, n, v, q, p);
           nU.setItem(k, n, v, q, p); //end edited code- this interface is killing me so I didn't update the rest of my code- but it looked like this
    
        mark = file.tellg();
        file.seekg(mark);
        getline(file, n);
        getline(file, v);
        file >> k;
        file >> q;
        file >> p;
        sW.setItem(k, n, v, q, p);
    
        mark = file.tellg();
        file.seekg(mark);
        getline(file, n);
        getline(file, v);
        file >> k;
        file >> q;
        file >> p;
        vsP.setItem(k, n, v, q, p);
    
    file.close();
        cout << "here is the object we extracted from 'file': " << endl;
          pshop.printItem();
          nU.printItem();
          sW.printItem();
          vsP.printItem();
    
    classVector.push_back(pshop);
    classVector.push_back(nortonU);
    classVector.push_back(vsPro);
    classVector.push_back(nSysWorks);
    

    以下是输出:

    键:5
    名称:Adobe Photoshop
    版本:CS5
    数量:21
    价格:580美元

    键:1
    名称:lities
    版本:不适用
    数量:10
    价格:50美元


    键:3
    名称:系统工程
    版本:2009
    数量:6
    价格:50美元


    键:4
    姓名:al Studio Professional
    版本:2010
    数量:19
    价格:700美元

    我试过cin。ignore()以尝试获取被截断的其余标题,并同时获取cin。clear(),你可能会猜到这只会让事情变得更糟。我知道这是很基本的东西。谢谢你帮我渡过难关。

    1 回复  |  直到 7 年前
        1
  •  0
  •   molbdnilo    7 年前

    混合 getline >> 因为你需要小心换行,所以经常会出错。
    使用 seekg 在文本文件上也打开了一个巨大的蠕虫罐头,看起来你只是偶然发现了它。

    getline公司 并使用 std::istringstream 在包含多个项目的行上:

    getline(file, n);
    getline(file, v);
    std::string kqpline;
    getline(file, kqpline);
    std::istringstream kqpstream(kqpline);
    kqpstream >> k >> q >> p;
    pshop.setItem(k, n, v, q, p);
    

    你应该把它抽象成一个函数。
    这应该按原样工作:

    ItemClass readItem(std::istream& in)
    {
        std::string name;
        std::string version;
        std::getline(in, name);
        std::getline(in, version);
        std::string line;
        std::getline(in, line);
        std::istringstream data(line);
        // Just guessing on the types here.
        // Adjust according to reality.
        unsigned int key = 0;
        unsigned int quantity = 0;
        float price = 0;
        data >> key >> quantity >> price;
        ItemClass result;
        result.setItem(key, name, version, quantity, price);
        return result;
    }
    
    int main()
    {    
        //declare a vector to hold the objects for later sorting and placing into a tree structure
        vector<ItemClass> classVector;
        ifstream file("software.txt");
    
        if (file.fail()) {
            cout << "Error reading from file." << endl;
            return -1;
        }
    
        ItemClass pshop = readItem(file);
        ItemClass nU = readItem(file);
        ItemClass sW = readItem(file);
        ItemClass vsP = readItem(file);
        cout << "here is the object we extracted from 'file': " << endl;
        pshop.printItem();
        nU.printItem();
        sW.printItem();
        vsP.printItem();
    
        classVector.push_back(pshop);
        classVector.push_back(nortonU);
        classVector.push_back(vsPro);
        classVector.push_back(nSysWorks);
    }
    

    (这段代码中没有错误处理,因为这会让人分心。真正的代码应该处理错误。)