Using the ios::binary
flag doesn't necessarily mean that you read and write binary data. Take a look at https://stackoverflow.com/a/2225612/2372604 . ios::binary
means "data is read or written without translating..."
What you probably want to do is use s.read(...)
. In your case the stream operator attempt to read a complete integer (something like "1234") rather then X number of bits that will fit into your integer.
For reading 4 bytes, something like the folling might work (untested):
int n;
while (s.read((char*) &n, 4) && s.gcount() != 0 ) {}