-4

Hi i have done a simple LZW compressor in C but i couldn't find anything about how to write compressed values to a file.

For example i compressed a txt file which is "banana bandana" and the output code is :

98 97 110 257 97 32 256 110 100 259

How should i write this into a new file? I need to read it again with C and decompress it.

koray
  • 1
  • 1
    Create/open the new file, write the values in as they become available, close the file. – Martin James Dec 23 '20 at 09:09
  • 1
    What is `98 97 110 257 97 32 256 110 100 259`? Is it a string? Is it an array of `int`, `short` or whatever? The question is incomplete, please [edit] and tell us more, and maybe show us some relevant parts of your code. – Jabberwocky Dec 23 '20 at 09:44
  • 1
    Each output code _also_ has a number of bits associated it with. You write that number of _bits_ to the output file, where those bits contain the code. To write bits to a file, you accumulate bits in an integer "buffer". Whenever you have eight or more, you write the first eight to a file as a byte, and pull them out of the buffer. – Mark Adler Dec 23 '20 at 17:42

1 Answers1

-1

I guess so, you should write your compressed data to another file, say 'banana.lzx'.

Here is how to write the compressed data to the file:

#include <stdlib.h>
int compressed_data[10] = {98, 97, 110, 257, 97, 32, 256, 110, 100, 259};
FILE * fp = fopen("banana.lzx", "w");
fwrite(compressed_data, sizeof(compressed_data[0]), sizeof(compressed_data), fp);
fclose(fp);

Here is how to read the compressed data from the file:

#include <stdlib.h>
int compressed_data[10] = {0};
FILE * fp = fopen("banana.lzx", "r");
fread(compressed_data, sizeof(compressed_data[0]), sizeof(compressed_data), fp);
fclose(fp);
/* compressed_data is retrieved, and can now be decompressed */

Remark: I don't know anything about LZW compression, but shouldn't the compressed data be a set of bytes? If so, why are you getting values above 255 (cf. 257, 256 and 259) ?

Lrnt Gr
  • 369
  • 2
  • 9
  • 2
    Should use binary mode when opening the file so it doesn't break on Windows. – Shawn Dec 23 '20 at 09:33
  • 1
    You're right about the LZW knowledge but I thought I could help right away. Still, no matter the byte alignment, you end up getting bytes as a compression result, don't you? – Lrnt Gr Dec 23 '20 at 09:34
  • @user3386109 actually the LZW aspect is irrelevant for the questrion. The question is actually: _How to write binary data to a file_. – Jabberwocky Dec 23 '20 at 09:37
  • 1
    @user3386109 no I haven't. But the question is just about writing data to a file. After all compressed data ist just bytes. Or maybe the question is incomplete. – Jabberwocky Dec 23 '20 at 09:39
  • Will this not write sizeof(int) times too much? – Martin James Dec 23 '20 at 13:02
  • @MartinJames You're right, this actually writes 10 times an 'int' in the file: assuming sizeof(int)=4 the file is then 4*10=40B long, while the initial data was 14B long, so not quite a great compression! That's why I was expecting the compression result to be a set of 10 bytes, but some values are overflowing byte capacity (> 255). – Lrnt Gr Dec 23 '20 at 13:11