0

enter image description here

I am trying to read character by character in C++ from a file. To the baked eyes the file has only "ABCD" content.

However, when I read the character by character from the file using ifstream (through the get() function), I see these extra characters.

I am not able to understand what is the issue. Kindly give some suggestions.

Below code to read byte by byte from the file input_file -> ifstream

while(input_file.good()){ unsigned char c = input_file.get() // other logic }

  • `while (input_file.good())` is basically the same as `while (!input_file.eof())`, hence the duplicate. – Some programmer dude Nov 19 '22 at 08:36
  • 1
    You misunderstand how `good` works. `good` is used to test if a stream is good **after** you have read something, not before you have read something. It's doesn't tell you that the next read will be OK, only that the last read was OK. Therefore `good` does not tell you whether there is a character available to read (and neither does `eof`). – john Nov 19 '22 at 08:38

0 Answers0