0

My friend is working on a neural network project. Copying some code from this tutorial, he is using this part of the code for reading lines of numbers from a file into vectors:

void TrainingData::getTopology(vector<unsigned> &topology)
{
    string line;
    string label;

    getline(m_trainingDataFile, line);
    stringstream ss(line);
    ss >> label;
    if (this->isEof() || label.compare("topology:") != 0) {
        abort();
    }

    while (!ss.eof()) {
        unsigned n;
        ss >> n;
        topology.push_back(n);
    }

    return;
}

When compiling and running this using MinGW, the lines of numbers are read correctly. But when compiling and running this using WSL, the last number is repeated, i.e. reading 3 2 1 results in 3 2 1 1 being stored in the vector.

Why does it repeat with WSL? Shouldn't the EOF bit have been set once last number is read, so that it breaks out of the while loop?

Jayce444
  • 8,725
  • 3
  • 27
  • 43
  • 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) – Jerry Jeremiah Nov 11 '21 at 23:41
  • @JerryJeremiah while that does explain why it's not a good idea, it doesn't actually answer my question, which is why they differ between MinGW vs. WSL – Jayce444 Nov 11 '21 at 23:48
  • but...the reason it's a bad idea is because it might differ – Garr Godfrey Nov 12 '21 at 00:13
  • `getline` should include the newline character. You haven't hit `eof` until you've read the last character (newline), which doesn't happen just reading numbers. My guess is a slight difference in data file: one has newline the other doesn't – Garr Godfrey Nov 12 '21 at 00:17

0 Answers0