4

So i'm reading in a input file that contains:

lololololololol

I need to read it in using binary one byte at a time for something I'm doing later on. To do this i'm using get() to read it in then storing it into a char. It seems to be working correctly except for the last char that it reads in. The vector that it is reading into contains:

lololololololol
�

I'm not quite sure what this last value is but it's totally throwing off my finial output. So my question is, is there a reason get() would read in a value or byte from my text document that is not there? Or is it reading in something that I don't know of?

code:

while(istr.good()) {
    temp = istr.get();
    input.push_back(temp);
}
LogicStuff
  • 18,687
  • 6
  • 49
  • 70
David Dennis
  • 604
  • 1
  • 7
  • 22
  • 2
    `get` returns `EOF` when the end of file is reached. `good` won't report end-of-file until that has happened. So the mysterious character is the EOF marker. – Jonathan Potter Feb 13 '16 at 23:02
  • 1
    @JonathanPotter Please don't answer in comments. – emlai Feb 13 '16 at 23:11
  • Textbook example of why `while (stream is good) { read from stream; use data }` is wrong. The stream isn't made un-good until _after_ you've tried to read past the end of the buffer. `.good()` doesn't tell the future. – Lightness Races in Orbit Feb 13 '16 at 23:18

1 Answers1

4

It's reading the EOF (end of file) character. You need to do the check after reading it to avoid it being inserted to the vector:

while(temp = istr.get(), istr.good()) // comma operator
    input.push_back(temp);

Or you might use the 2nd std::istream_base::get overload and let istr implicitly convert to bool:

while(istr.get(temp))
    input.push_back(temp);

Or try more advanced approaches. operator>> and std::getline would also work fine for this kind of input.

Community
  • 1
  • 1
LogicStuff
  • 18,687
  • 6
  • 49
  • 70