0

I have data files with extra empty lines in them at the end. This is causing problems with reading in the data lines. I'm using:

while (datFile.good())

but .eof() didn't work either.

Any suggestions?

    while (datFile.good())
    {
        getline (datFile, line);

        istringstream liness(line);

        int z = 0;

        while (z <= index)
        {
            while (liness >> temp)
            {
                if (z == 0)
                {
                    values[0].push_back(atof(temp.c_str()));
                }

                if (z == index)
                {
                    values[1].push_back(atof(temp.c_str()));
                }

                cout << temp << endl;

                z++;
            }
        }

    }
user1187621
  • 151
  • 1
  • 2
  • 16

2 Answers2

2

Thou shalt use the stream's status after thy read!

You didn't post enough code to tell but input always looks something like this:

while (in >> data) {
    process(data);
}

Whether you use formatted input or unformatted input diesn't matter either. Also, good() is generally not that useful aabd eof() is only useful to suppress an error message: if you read failed it may be OK that it failed because you read all the data. It is never a useful condition to determine whether a read was successful: eof() can yield true although the read was successful.

Robᵩ
  • 143,876
  • 16
  • 205
  • 276
Dietmar Kühl
  • 141,209
  • 12
  • 196
  • 356
2
while (getline (datFile, line))
{

    istringstream liness(line);

    // the rest of the loop is unchanged.
    ...
}
Robᵩ
  • 143,876
  • 16
  • 205
  • 276