-2

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.

demongolem
  • 9,474
  • 36
  • 90
  • 105
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Dec 21 '16 at 16:46
  • 2
    what is it writing and what is it reading? I don't get how you've concluded that. – Webert Lima Dec 21 '16 at 16:55

1 Answers1

1

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).

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
Dellowar
  • 3,160
  • 1
  • 18
  • 37