The code is very simple.
unsigned char a_byte;
ifstream a_file("C:/file.bin", ios_base::binary);
if (a_file.is_open() && a_file.good())
{
a_file.seekg(0);
a_file >> a_byte;
a_file.close();
}
The problem is that it won’t read 09h from a one-byte file – I just get zero in the a_byte var. It does work with different values. What’s the reason?
The stream classes’
operator>>will skip whitespace before reading into the target variable. Here the char value09his TAB, which is counted as whitespace and skipped.If you want to read every single character, try the
getfunction.