简体   繁体   中英

What format should I write my .txt file if I want to getline for C++ program?

I have this code in my main program. I do not know my text file should be formatted into what way to let my main getline from it.

//...
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();
//...

Say I have this list of book:

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

I would like to know where should be spaced, entered next line and so on, so that my file can be read in line by line, and output as a booklist to user when they select that.

You can actually leave your file as is, with commas as delimiters. You just have to specify different delimiter with getline .

std::getline(input, target_string, ',')

where target_string is book[index].author, title or other attributes.

For the last attribute on the line you would use this:

std::getline(input, target_string)

Newline is default delimiter.

You can use std::getline and std::stringstream to read in all the books.

Notice that you are missing the genre property in the file.

Here is a sample program with comments to do just that, I inferred the classes from your code, if something is not accurate you can refactor the code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM