0

I am working on a coding project where I sort and organize data from a text file, and I cannot get the getline() function to read past the first line.

The idea is to capture the entire line, split it into 3 sections, assign it to an object, then move on. I can do everything except get getline() to work properly, here is the snippet of code I am having trouble with:

ifstream fin;
fin.open("textFile.txt");
while (!fin.eof()) // while loop to grab lines until the end of file is reached
{
    getline(fin, line); 
    fin >> first >> last >> pace; // assigning the data to their respective variables
    ClassObject obj(first, last, pace); // creating an object with those variables
    ClassVector.push_back(obj); // assignment object to vector
}

This has been the closest I have gotten to reading every line while also sorting the data into a vector, but as I mentioned before, getline() will read line 1, and skip the rest of the file (1000 lines).

rioV8
  • 12,369
  • 2
  • 12
  • 26
  • How are `first`, `last` and `pace` defined? – DarkAtom May 07 '21 at 06:14
  • They are defined earlier in the code: string first, string last, int pace; – MajesticMilkMan May 07 '21 at 06:21
  • The first iteration of the `while` will read the first line with `getline`, and then the next `fin >>` will read those 3 variables, and probably finish the next line. The second `getline` will eat the leftover `\n`, and the `fin >>` will read the third line. Next, `getline` eats the leftover `\n` etc. – DarkAtom May 07 '21 at 06:33
  • You're reading a `line` and then doing nothing with it. You then proceed to read `first`, `last` and `pace` _from the next line_. If that fails then the stream has an error and `eof` will return true, stopping the loop. – acraig5075 May 07 '21 at 06:33
  • Okay, I see, how would I approach fixing this? I am unsure how to capture the first, last, and pace sections of each line otherwise – MajesticMilkMan May 07 '21 at 06:38
  • I got it now!! Thank you guys so much! This has been such a headache to deal with, and for such a simple solution. – MajesticMilkMan May 07 '21 at 06:41
  • issue 1: https://stackoverflow.com/a/5605159/1387438 – Marek R May 07 '21 at 07:02
  • issue 2: https://stackoverflow.com/a/21567292/1387438 – Marek R May 07 '21 at 07:06
  • 2
    In summary: ditch the `eof` check (there's a failr chance that you will never find a good use for `eof` in your entire career) and write `while (fin >> first >> last >> pace) { ... }`. – molbdnilo May 07 '21 at 07:28
  • if you use a debugger you might find the problem yourself – rioV8 May 07 '21 at 09:58

1 Answers1

0

what you can do is rather than using !fin.eof(). i prefer to use something similar to this:

    ifstream file ( fileName.c_str() );

    while (file >> first >> last >> pace ) // assuming the file is delimited with spaces
    {
        // Do whatever you want with first, last, and pace
    }

The "While loop" will keep reading the next line until we reach the end of the file.

If the lengths of first, last, pace are constant, you can also just get the contents of the line (in a string variable) and use substring on it, but this only works in the specific case that the lengths are constant throughout the entire file.