Inconsistency with inttypes.h, fscanf(), fprintf()

183 views Asked by At

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.

1

There are 1 answers

3
Sergey Kalinichenko On BEST ANSWER

The problem is that the values in your text file end up being merged together, like this:

8100200

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:

fprintf(f, "%"PRIu8" %"PRIu16" %"PRIu16, bs, bw, bh);

[fscanf is] supposed to read a 1 byte value then two 2 bytes

fscanf reads text, not bytes. If you would like to write bytes, use library functions for binary output and input, i.e. fwrite and fread:

// Writing in binary
f = fopen("out.bin", "wb");
fwrite(&bs, sizeof(bs), 1, f);
fwrite(&bw, sizeof(bw), 1, f);
fwrite(&bh, sizeof(bh), 1, f);
fclose(f);
// Reading in binary
f = fopen("out.bin", "rb");
fread(&bs, sizeof(bs), 1, f);
fread(&bw, sizeof(bw), 1, f);
fread(&bh, sizeof(bh), 1, f);