Do not use formatted functions such as * scanf; they expect character representations of data, the addition of * printf functions that translate values into representations of characters.
unsigned val; f = fopen (filename, "rb"); if (fread (&val, 1, sizeof (val), f) != sizeof (val))
The biggest caveat is how the file was written. If the author’s continent is different from the computer on which it is running, then an explicit user code should be used instead:
unsigned val; unsigned char buf[2]; f = fopen (filename, "rb"); if (fread (buf, 1, sizeof (buf), f) != sizeof (buf))
source share