https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69218

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Comment on attachment 37296
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=37296
The test case and example text file which doesn't ends with the newline
character.

N.B. it's not useful to upload a tar file containing two tiny files, just
attach the files directly, or paste them straight in.

// When EOF reached while reading a text file _which doesn't ends
// with newline character_ the stream's state becomes
// "not good" and "eof" but remains "not fail".

#include <cassert>
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char *argv[])
{
  // The file must not terminates with the newline character.
  const char* file = "test.txt";
  std::ifstream in(file);
  if (!in) {
    std::cerr << "Could not work with file " << file << std::endl;
    return 1;
  }

  while (in) {
    std::string w;
    in >> w;
    std::cerr << "Fail: " << in.fail()
              << "   Good: " << in.good()
              << "   EOF: " << in.eof()
              << std::endl;
    // Assertion failed if the file doesn't ends with newline character.
    assert(!in.fail() == in.good());
  }

  return 0;
}

Reply via email to