c++ - 如果我要为C++程序获取行号,应该以什么格式写入.txt文件?

标签 c++ fstream getline

我的主程序中有此代码。
我不知道我的文本文件应格式化为哪种方式,以便让我的主getline从中删除。

//...
BOOK_TYPE book[100];
int index = -1, choice;

input.getline(book[++index].isbn, 14);

while (input)
{
    input.getline(book[index].author, 20);
    input.getline(book[index].title, 30);
    input.getline(book[index].publisher, 20);
    input.getline(book[index].year, 5);
    input.getline(book[index].price, 10);
    input.getline(book[index].quantity, 103);

    for (int k = 0; k < 5; k++)
    {
        input.getline(book[index].category[k].cor_x, 2);
        input.getline(book[index].category[k].cor_y, 2);
        input.getline(book[index].category[k].genre, 20);
    }
    // clear unwanted whitespace
    if (input.peek() == '\n')
        input.ignore(256, '\n');
    // read next number
    input.getline(book[++index].isbn, 14);
}
input.close();
//...

说我有这本书的 list :
9780809875214,John Wick,The assasinate of a Gang,Tree Production,2014,39.00,4,2,2
9788373191723,J.R.R Tolkien,The Lord of the Rings,Allen & Unwin,1954,120.45,6,3,1
9783791535661,Lewis Carroll, Alice's Adventure in Wonderland,Macmillan Publishers,1865,100.25,5,3,2
9781517322977,Mikhail Bulgakov,The Master and Margartia,Penguin Books,1967,125.00,7,3,3
9780676516197,Vladmir Nabokov,Lolita,Olympia Press,1955,98.25,3,3,1
9781095627242,Anna Sewell,Black Beauty,The Jarrold Group,1877,60.25,2,3,2
9788497592581,Umberto Eco,The Name of the Rose,Bompiani,1980,45.65,7,1,3
9780062316110,Harari and Yuval Noah,Sapiens: A Brief History of Humankind,Harper Perennial,2018,18.06,2,1,3

我想知道应该在哪里隔开,输入下一行等等,以便可以逐行读取我的文件,并在用户选择该文件时将其作为书本输出给用户。

最佳答案

您可以使用 std::getline std::stringstream 阅读所有书籍。

请注意,您缺少文件中的genre属性。

这是一个带有注释的示例程序,我从您的代码中推断出这些类,如果有些不正确,则可以重构代码:

Running sample

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>

class Category {
public:
    int cor_x;
    int cor_y;
    std::string genre;
};

class BOOK_TYPE {
public:
    std::string isbn;
    std::string author;
    std::string title;
    std::string publisher;
    std::string year;
    double price;
    int quantity;
    Category category;
};

int main() {

    std::vector<BOOK_TYPE> books; //dynamic container for books

    BOOK_TYPE book;
    std::string temp, price, quantity, cor_x, cor_y, genre, tempin;
    std::fstream input;
    input.open("test.txt");

    if(!input.is_open()){
        std::cerr << "No file";
        return 1;
    }      

    while (std::getline(input, tempin)) //read each whole line till there are no more
    {
        std::stringstream temp(tempin); //stream format
        getline(temp, book.isbn, ','); //read all properties with commma separator
        getline(temp, book.author, ',');
        getline(temp, book.title, ',');
        getline(temp, book.publisher, ',');
        getline(temp, book.year, ',');
        getline(temp, price, ',');
        book.price = stod(price);
        getline(temp, quantity, ',');
        book.quantity = stoi(quantity);
        getline(temp, cor_x, ',');
        book.category.cor_x = stoi(cor_x);
        getline(temp, cor_y, ',');
        book.category.cor_y = stoi(cor_y);        
        getline(temp, book.category.genre);
        books.push_back(book);  //add book to the vector of books
    }
    input.close();

    //test print some of the properties in range based loop, you can print the rest
    // you can use the normal for loop with books[i] and i < books.size()
    for(auto book : books)
        std::cout << book.title << " " << book.author <<  " " << book.isbn << " " 
        << book.category.cor_y << " " << book.category.cor_x << " " 
        << book.price << " " <<  book.category.genre << std::endl;
}

关于c++ - 如果我要为C++程序获取行号,应该以什么格式写入.txt文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61209898/

相关文章:

c++ - 如何使用cmake在linux下构建Qt项目

C++ ifstream getline 不工作

c++ - 一次又一次尝试在给出错误路径的同时打开文件

c++ - 打开文件流输入的 Getline 问题

c++ - C++ 文件中的 vector

c++ - x86-64 Linux NASM。在 C++ 文件中声明为函数的类型 int 数组的函数参数传递

c++ - getline 不会从变量中读取字符串。 (C++)

c++ - istream::getline() 令人费解的行为

c++ - 是什么导致在电脑崩溃之前将空字符写入文件?

c++ - 如何使用 C++ 程序快速地将文件从一个位置复制到另一个位置?