Quantcast
Viewing latest article 10
Browse Latest Browse All 104

How to get a line of text in a ifstream file with another line of text?

I have a txt file that has page number data for 3 books. I am making a program to quickly change that data for a website. Here is my txt file:

0 31 572 173

The 0,1,2 is the book index's I would like to attach to the page number data. I want to take user input asking which book index I would like to get the page number data from.

Here is my code right now:

std::ifstream file("C:\\Users\\Robed\\OneDrive\\Documents\\Judah.site\\book_keep.txt");if (!file.is_open()){    std::cerr << "File could not be opened, make sure path is right!" << std::endl;    return false;}std::string line;while (getline(file, line)) {    std::cout << line << std::endl;}int inputBookIndex;std::cout << "Input book index to change page number of: ";std::cin >> inputBookIndex;if (inputBookIndex < 0){    std::cout << "Invalid Book Index!" << std::endl;    return false;}file.close();return true;

What I can't seem to figure out, though, is how do I assign the index to the page number to be able to do something like this:

Enter Book Index: 0Page Number: 3

I tried reading other discussions online, but none of them explained what I wanted, or went nowhere.


Viewing latest article 10
Browse Latest Browse All 104

Trending Articles