从文件和字符串中读取问题

时间:2019-04-15 14:48:49

标签: c++

我正在使用for循环从文件中读取数据并将值存储在类成员中:

while (!bookstxt.eof())
 {
    for (int i = 0; i < x; i++) {

        getline(bookstxt, title);
        getline(bookstxt, author);
        getline(bookstxt, publisher);
        getline(bookstxt, isbn);

        cin.clear();
        cin.ignore(100, '\n');

        bookstxt >> price >> year >> numInStock;

        cout << title << "\n" << author << "\n" << publisher << "\n" << isbn << "\n" << price << "\n" << year << "\n" << numInStock;

        bookList[i].storeBook(title, author, publisher, isbn, price, year, numInStock); 
    }
}

问题是ISBN部分存在问题-第一次在循环中执行了它需要正确执行的操作,但是第二次它弄乱了isbn并以某种方式将其推到下一个两个变量。 这是断行的输出结果

The World is Flat
Friedman, Thomas
Farrar, Straus and Giroux
0-374-29279-5
30.00
2006
12 

这是实际发生的情况:

Starting Out with C++
Gaddis, Tony
Pearson
978-0-13-257625-3
129.98
2014
25

The World is Flat
Friedman, Thomas
Farrar, Straus and Giroux
0
-374
-29279
-5
30.00
2006
12
0
-374
-29279

阅读ISBN部分似乎有些麻烦。 不胜感激

2 个答案:

答案 0 :(得分:0)

您的cin.ignore()通话位置不正确。 getline()吃掉了结尾的换行符,因此没有什么可忽略的。 >>运算符没有,因此您需要忽略它留在流中的空白。使用cin.ignore()运算符后,将>>移至:

while (!bookstxt.eof())
 {
    for (int i = 0; i < x; i++) {

        getline(bookstxt, title);
        getline(bookstxt, author);
        getline(bookstxt, publisher);
        getline(bookstxt, isbn);

        bookstxt >> price >> year >> numInStock;
        cin.ignore(100, '\n');

        cout << title << "\n" << author << "\n" << publisher << "\n" << isbn << "\n" << price << "\n" << year << "\n" << numInStock;

        bookList[i].storeBook(title, author, publisher, isbn, price, year, numInStock); 
    }
}

此外,在循环条件中使用.eof()Bad Idea

答案 1 :(得分:0)

这是一个解决方案。它不会在循环中使用.eof(),而使用stringstream来提取数值。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main () {

    string title, author, publisher, isbn, empty_line;
    string string_price, string_year, string_stock;
    double price;
    int year, stock;
    ifstream bookstxt ("books.txt");
    if(bookstxt.is_open()) {
        while(getline(bookstxt,title))
        {
            getline(bookstxt, author);
            getline(bookstxt, publisher);
            getline(bookstxt, isbn);
            getline(bookstxt, string_price);
            getline(bookstxt, string_year);
            getline(bookstxt, string_stock);
            getline(bookstxt, empty_line);

            stringstream(string_price) >> price;
            stringstream(string_year) >> year;
            stringstream(string_stock) >> stock;

            cout << title << "\n" << author << "\n" << publisher << "\n" << isbn << "\n" << price << "\n" << year << "\n" << stock << "\n\n";
        }
        bookstxt.close();
    } else {
        cout << "Unable to open file";
    }

    return 0;
}

我知道您的原始books.txt文件应如下所示,如果我写错了,请纠正我:

Starting Out with C++
Gaddis, Tony
Pearson
978-0-13-257625-3
129.98
2014
25

The World is Flat
Friedman, Thomas
Farrar, Straus and Giroux
0-374-29279-5
30.00
2006
12
0

祝你好运!