I'm having some problems with inttypes, illustrated here by this tiny code sample:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
void print_byte(uint8_t b)
{
printf("%d%d%d%d%d%d%d%d\n",
!!(b & 128), !!(b & 64), !!(b & 32), !!(b & 16),
!!(b & 8), !!(b & 4), !!(b & 2), !!(b & 1));
}
int main()
{
FILE *f;
uint8_t bs = 8;
uint16_t bw = 100, bh = 200;
f = fopen("out", "w+");
print_byte(bs);
printf("%"PRIu8" %"PRIu16" %"PRIu16"\n", bs, bw, bh);
fprintf(f, "%"PRIu8"%"PRIu16"%"PRIu16, bs, bw, bh);
fclose(f);
f = fopen("out", "r");
fscanf(f, "%"SCNu8"%"SCNu16"%"SCNu16, &bs, &bw, &bh);
printf("%"PRIu8" %"PRIu16" %"PRIu16"\n", bs, bw, bh);
print_byte(bs);
fclose(f);
return 0;
}
Gives me
gcc -o test test.c && ./test
00001000
8 100 200
104 100 200
01101000
If I change the SCNu8
to SCNo8
in the fscanf, I get what I'm supposed to get:
00001000
8 100 200
8 100 200
00001000
Where is the problem? I don't see why it doesn't work for the first code, but works when I interpret that byte as an octal value.
The problem is that the values in your text file end up being merged together, like this:
That is why you cannot read the data back correctly:
fscanf
does not know where the first number ends and the next number starts.Putting spaces into the
fprintf
format line fixes this problem:fscanf
reads text, not bytes. If you would like to write bytes, use library functions for binary output and input, i.e.fwrite
andfread
: