binary mode is correct and is 100% necessary when writing binary data.. Without it, your output will be mangled by newline transformations.
Your other problem is that you are not properly reading back what you are writing. This is tricky because you are mixing textual output and binary output. I recommend not doing that -- pick one or the other and run with it.
Note: if you do not have a hex editor... get one. They are a godsend for when you're working with binary files. HxD is a fantastic free hex editor.
Assuming you change the code to output binary data... if you look at the generated file in a hex editor, you get this:
|
32 20 FF FF FF 8A 00 00 04 0A 20
|
The first '32' is your value of '2' stored as text.
(0x32 == '2')
The '20' is the space
(0x20 == ' ')
The 'FF FF FF 8A' is your -118 stored correctly in binary (though big endian? ew)
The '00 00 04 0A' is your 1034 stored in binary
And the final '20' is another space
The problem is your tellg() call on line 73 is sort of lying to you and returning 0 because... well... I'm not sure why. Probably because it's meant to be used with binary files and not with text files.
So anyway, when you open the file again with fopen() on line 78, you seek to position
0 on line 80...
not to position 2 like you would need to in order for this to work properly.
The end result is that you are reading the 2 and space as part of your first number, which throws the whole thing off.