C 基本ifstream seekg()未加载

c++ basic ifstream seekg() not loading

本文关键字:加载 seekg 基本 ifstream      更新时间:2023-10-16

我一直在努力从文件中导入我的对象2天以上,阅读本书,搜索互联网并基本上尝试了我能想到的一切。最后一条沟(当然我永远不会放弃哈哈)。>>>我有一个用于软件对象的类项目类,我将其放在矢量中最终要更新并写回文件。我的完美结果是将文件的内容直接读取到软件对象的向量中,但现在我只想正确获取文件的内容。我正在阅读的文件是:

Adobe Photoshop
CS5
5 21 580
诺顿公用事业
n/a
1 10 50
诺顿系统的工作
2009
3 6 50
Visual Studio Professional
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
版本:N/A
数量:10
价格:$ 50


钥匙:3
名称:系统作品
版本:2009
数量:6
价格:$ 50


钥匙:4
名称:AL Studio Professional
版本:2010
数量:19
价格:$ 700

我尝试cin.ignore()尝试获取其余的截断和cin.clear()的标题,您可能会猜测这使情况变得更糟。我知道这是非常基本的东西。谢谢您在这个驼峰上帮助我。

混合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);

,您应该将其抽象成一个函数。
这应该像IS一样工作:

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

(此代码中没有错误处理,因为这主要是分散注意力的。实际代码应处理错误。)