Very simple binary file compressor in C
-
Hello as subject says i am trying to make some simple binary file compressor (for now). I started with basics. This code opens file in binary mode reads byte by byte and input bytes writes to output "compressed" file. If byte == 0x00 counts number of 0x00's and writes 0x00 and number of 0x00's.
#include
#includeint main()
{
int i, duzina, stanje=0, brojac=0;
unsigned char chr;
FILE* ulaz = fopen("../testapp/testapp", "rb");
FILE* izlaz = fopen("../testapp/testapp.zip", "wb");if(ulaz == NULL) { printf("Problem with file!\\n"); exit(1); } if(izlaz == NULL) { printf("Problem with output file!\\n"); exit(1); } fseek(ulaz, 0, SEEK\_END); duzina = ftell(ulaz); fseek(ulaz, 0, SEEK\_SET); for(i=0; i
To "decompress" file i use this code which as input have "compressed" file and read byte by byte if byte == 0x00 reads integer and write N times 0x00 (N value = readed integer)
#include
#includeint main()
{
int i,j, duzina, brojac=0;
unsigned char chr;FILE\* ulaz = fopen("../testapp/testapp.zip", "rb"); FILE\* izlaz = fopen("../testapp/testapp1", "wb"); if(ulaz == NULL) { printf("Problem with file!\\n"); exit(1); } if(izlaz == NULL) { printf("Problem with output file!\\n"); exit(1); } fseek(ulaz, 0, SEEK\_END); duzina = ftell(ulaz); fseek(ulaz, 0, SEEK\_SET); for(i=0; i
So my problem is that when "decompressing" is finshed some bytes are missing (program works correct).
Byte comparing:
[stone@hero testapp]$ diff testapp1.hex testapp.hex
443,444c443,444
< 0001c00 0001
< 0001c010001c00 0001 0000 0000 0000 0000 0000 0000 0000
0001c10
[stone@hero testapp] -
Hello as subject says i am trying to make some simple binary file compressor (for now). I started with basics. This code opens file in binary mode reads byte by byte and input bytes writes to output "compressed" file. If byte == 0x00 counts number of 0x00's and writes 0x00 and number of 0x00's.
#include
#includeint main()
{
int i, duzina, stanje=0, brojac=0;
unsigned char chr;
FILE* ulaz = fopen("../testapp/testapp", "rb");
FILE* izlaz = fopen("../testapp/testapp.zip", "wb");if(ulaz == NULL) { printf("Problem with file!\\n"); exit(1); } if(izlaz == NULL) { printf("Problem with output file!\\n"); exit(1); } fseek(ulaz, 0, SEEK\_END); duzina = ftell(ulaz); fseek(ulaz, 0, SEEK\_SET); for(i=0; i
To "decompress" file i use this code which as input have "compressed" file and read byte by byte if byte == 0x00 reads integer and write N times 0x00 (N value = readed integer)
#include
#includeint main()
{
int i,j, duzina, brojac=0;
unsigned char chr;FILE\* ulaz = fopen("../testapp/testapp.zip", "rb"); FILE\* izlaz = fopen("../testapp/testapp1", "wb"); if(ulaz == NULL) { printf("Problem with file!\\n"); exit(1); } if(izlaz == NULL) { printf("Problem with output file!\\n"); exit(1); } fseek(ulaz, 0, SEEK\_END); duzina = ftell(ulaz); fseek(ulaz, 0, SEEK\_SET); for(i=0; i
So my problem is that when "decompressing" is finshed some bytes are missing (program works correct).
Byte comparing:
[stone@hero testapp]$ diff testapp1.hex testapp.hex
443,444c443,444
< 0001c00 0001
< 0001c010001c00 0001 0000 0000 0000 0000 0000 0000 0000
0001c10
[stone@hero testapp]The compressor has a problem with a possibly pending 0-sequence at the end of the file, because your program outputs the 0-sequences only on changes, that is when meets a non-zero character. You have to check for a pending 0-sequence immediately after execution of the for loop:
if(stanje != 0)
{
fputc(0x00, izlaz);
fwrite(&brojac, sizeof(int),1,izlaz);
}