binary - C- Run-length encoding doesn't work on large file, worked on smaller file -
i have problem, when utilize on 41 kb file compressed (although because uses run-length encoding seems double size of file) , decompresses properly. when tried utilize on 16,173 kb file , decompressed it, didn't open , file size 16,171 kb....so decompressed didn't homecoming it's original form....something screwed up....has me baffled, can't seem figure out doing wrong....
the method used run-length encoding, replaces every byte count followed byte.
before:
46 6f 6f 20 62 61 72 21 21 21 20 20 20 20 20
after:
01 46 02 6f 01 20 01 62 01 61 01 72 03 21 05 20
here code:
void compress_file(file *fp_in, file *fp_out) { int count, ch, ch2; ch = getc(fp_in); (count = 0; ch2 != eof; count = 0) { // if next byte same increment count , test 1 time again { count++; // set binary count ch2 = getc(fp_in); // set next variable comparing } while (ch2 != eof && ch2 == ch); // write bytes new file putc(count, fp_out); putc(ch, fp_out); ch = ch2; } fclose(fp_in); fclose(fp_out); fprintf(stderr, "file compressed\n"); } void uncompress_file(file *fp_in, file *fp_out) { int count, ch, ch2; (count = 0; ch2 != eof; count = 0) { ch = getc(fp_in); // grab first byte ch2 = getc(fp_in); // grab sec byte // write bytes { putc(ch2, fp_out); count++; } while (count < ch); } fclose(fp_in); fclose(fp_out); fprintf(stderr, "file decompressed\n"); }
you miss check run length count char overflow, go wrong if have more 255 identical characters in file in sequence.
c binary run-length-encoding
No comments:
Post a Comment