1

I am trying to read from a file like this

1 23 5 15
3 18 8 6 11

But then whenever it goes to the last string "15", it also reads "3" from then next line.

Here is my code for this part:

ifstream myFileRent;
    myFileRent.open("rented.txt");
    if (!myFileRent.is_open())
    {
        cout << "ERROR: File is corrupted or does not exists!";
    }
    while (!myFileRent.eof())
    {
        getline(myFileRent, customerID, ' ');
        while (getline(myFileRent, video_ID, ' '))
        {
            InsertCusRent(stoi(customerID), video_ID);
        }
    }

This is what it shows in debug when I am in the "15" before the next line "3"

video_ID    :  "15\n3"    :    std::string

Basically, first digit of the line will go to customerID, and every next digits will be pushed in to stack, which is why I used while because every line are not equal in length.

Ken White
  • 123,280
  • 14
  • 225
  • 444
JustR
  • 121
  • 5
  • 2
    Read a full line, parse the line using a `std::stringstream`, repeat until reading a line fails. – Retired Ninja Jul 06 '21 at 02:52
  • You're terminating a line with ' ' rather than '\n'. – robthebloke Jul 06 '21 at 02:53
  • 1
    Does this answer your question? [Why is iostream::eof inside a loop condition (i.e. \`while (!stream.eof())\`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – prehistoricpenguin Jul 06 '21 at 02:54
  • 1
    This is from the docs for `getline()`: `Extracts characters from [miFileRent] and stores them into [customerID] until the delimitation character delim is found`. So like the others said, first `getline` and then `split` in a next step. – DarkTrick Jul 06 '21 at 02:54
  • Thanks everyone! I have included my updated code above, problem solved! ;) – JustR Jul 06 '21 at 03:12
  • 1
    I've rolled back your edit. It is not acceptable here to add the solution into your question. If you've found a solution to your problem and want to share it, do so by writing an answer in the space below provided for that purpose. See [Can I answer my own question?](http://stackoverflow.com/help/self-answer). – Ken White Jul 06 '21 at 03:46

2 Answers2

0

Thanks for every comments, code is finally working!

string line, customerID, video_ID;
ifstream myFileRent;
    myFileRent.open("rented.txt");
    if (!myFileRent.is_open())
    {
        cout << "ERROR: File is corrupted or does not exists!";
    }
    while (!myFileRent.eof())
    {
        getline(myFileRent, line);
        stringstream ss(line);
        getline(ss, customerID, ' ');
        while (getline(ss, video_ID, ' '))
        {
            if (customerID != " ")
            {
                InsertCusRent(stoi(customerID), video_ID);
            }
        }
    }
JustR
  • 121
  • 5
  • 1
    You are still [using `eof()` the wrong way](https://stackoverflow.com/questions/5605125). `while (!myFileRent.eof()) { getline(myFileRent, line); ... }` needs to be `while (getline(myFileRent, line)) { ... }` – Remy Lebeau Jul 06 '21 at 05:45
  • 1
    instead of `while (!myFileRent.eof()) { getline(myFileRent, line);` use `while (getline(myFileRent, line)) {` – Jerry Jeremiah Jul 06 '21 at 05:47
0

I would do it like this:

string line;
ifstream myFileRent;
myFileRent.open("rented.txt");
if (!myFileRent.is_open())
{
    cout << "ERROR: File is corrupted or does not exists!";
}

// Don't use eof here.
// Even if it is not eof does not mean that there is data to read.
// The next read may still fail. So try and read the data if it
// fails the while loop will not enter.
while (getline(myFileRent, line))
{
    // You have a line. Lets use it in a stream.
    stringstream ss(std::move(line));

    // You convert customerID into an integer. Just
    // make it an integer now.
    int          customerID;
    std::string  video_ID;

    // Use the operator>> to read the customer ID into an integer.
    ss >> customerID;

    // To read simple white space separated values use operator>>
    // It will read ints or string as appropriate.
    while (ss >> video_ID)
    {
        InsertCusRent(customerID, video_ID);
    }
}

PS. You can get your WORKING code reviewed once working at https://codereview.stackexchange.com/

Martin York
  • 257,169
  • 86
  • 333
  • 562