fprintf(long) writes 8 bytes and fscanf(long) reads 6 bytes why?

76 views Asked by At

I'm writing a Huffman algorithm and when I write my file header, I store the length of my file because there will be some spare bits and I need to know where to stop.

This happens instead when I write the length of my file: It writes 8 bytes, but when I read, it reads only 6.

long totChar;
long size;

fprintf(outfile, "%ld", totChar);

fscanf(cmpfile, "%ld", &size);

I'm sure that works because if I add for example:

fgetc(cmpfile); \\compressed file
fgetc(cmpfile);

and then I start reading, the decompression is successful.

1

There are 1 answers

0
Dellowar On

You're reading and writing characters, not binary.

For example, maybe when you write data, you write the number 57,843,249 (8 digits). But when you read data, you read 875,345 (6 digits).