fscanf fails to return a proper result
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
This is the code example:
FILE *f_Extract;
int nMagicByte = 0x0;
f_Extract = fopen("Dt021546.mde", "rb");
fscanf(f_Extract, "%d", &nMagicByte);
fclose(f_Out);
The file contains the following first bytes:
D8 00 01 00 00...
The variable MagicByte will not contain the expected
value 0xD800 (55,296 decimal).
Logged In: NO
Surely it should return 0xd8000100 or 65755 as this is a
32bit request?
Logged In: NO
Program is flawed.
The fscanf function converts from text representations so MagicByte would only contain 55,296 if the file started with the ASCII string "55296". As it is MagicByte will be set to 0 as the the 0xD8 character is not valid in a string that represents an integer.
This is not a bug in Dev-C++ nor is it a bug in the underlying compiler, it is a fundament mistake in the program.