Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does getline() not read the last int value in the line?

I have a file that contains a list of books. Each book has 4 variables delimitated by a Tab.

The variables are

  • Title
  • author
  • isbn
  • qty

and there are around 400 book entries.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have managed to write a function that reads each input to an array and saves it to a Book object.

void loadFile() {
  
    std::ifstream inputFile("books");
    std::string bookArray[4];
    std::string line;
    Book book;
    while (getline(inputFile, line)) {
        size_t field2 = 0;
        size_t field1 = line.find("\t");
        for (int i = 0; i <= 3; i++) {
            size_t field1 = line.find("\t" , field2);
            if (field1 != std::string::npos) {
                std::string str2 = line.substr(field2, field1 - field2);
                bookArray[i] = str2;
            }
            field2 = field1 + 1;
        }
        
        book.setTitle(bookArray[0]);
        book.setAuthor(bookArray[1]);
        book.setIsbn(bookArray[2]);
        book.setQty(stoi(bookArray[3]));
  std::cout << "Book:   " <<book.getTitle <<'\t'<< book.getQty<< " records.\n";
}

but the function seems to not pick up the qty numbers at the end of each line. Everything else seems to be fine except the qty numbers. I cant find what’s wrong with the function.

I have tried implementing the same getline() function with a ‘\t’ delimitator argument like so:

while(getline(input, line, '\t')) {

The output just read the title of the first line and stopped there. I know the first function works.
Why is it not picking up the qty values?

The books file format is as follows:

A First Look at Graph Theory    Clark John; Holton Derek Allan  9788170234630   5
A Practical Approach To Data Structures And Algorithms  Sanjay Pahuja   9788122420678   7
A Practical Approach to High-Performance Computing  Sergei Kurgalin; Sergei Borzunov    9783030275587   6
A Practical Guide to UNIX for Mac OS X Users    Mark G. Sobell; Peter Seebach   9780321629982   5
A River Marc Martin 9781452162256   4

Total Noob here so I apologize if my code is awkward.

>Solution :

The reason is that there’s (likely) no tab character at the end of the line after the qty. So when you call line.find("\t", field2) the 4th time, it returns string::npos and you don’t store anything in bookArray[3]

The best fix is probably to put an else there with the if:

    } else {
        std::string str2 = line.substr(field2, std::string::npos);
        bookArray[i] = str2;
        break;  // there can be no more fields on the line

be aware that if you have a line with fewer than 4 fields, the elements of bookArray for the unfound fields will not be touched (so will continue to have values from the previous line)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading