0

我有一个问题,当我在一个 41 kb 的文件上使用它时,它被压缩(尽管因为它使用运行长度编码,它似乎总是使文件大小加倍)并正确解压缩。但是,当我尝试在一个 16,173 kb 的文件上使用它并解压缩它时,它没有打开,文件大小为 16,171 kb ......所以它解压缩了它,但它没有恢复到原来的形式.. ..有些事情搞砸了....让我感到困惑,我似乎无法弄清楚我做错了什么....

使用的方法是游程编码,它将每个字节替换为后跟字节的计数。

前:

46 6F 6F 20 62 61 72 21 21 21 20 20 20 20 20

后:

01 46 02 6F 01 20 01 62 01 61 01 72 03 21 05 20

这是我的代码:

    void compress_file(FILE *fp_in, FILE *fp_out)
    {
        int count, ch, ch2;

        ch = getc(fp_in);
        for (count = 0; ch2 != EOF; count = 0) {
            // if next byte is the same increase count and test again
            do {
                count++;           // set binary count
                ch2 = getc(fp_in); // set next variable for comparison
            } while (ch2 != EOF && ch2 == ch);
            // write bytes into 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;

        for (count = 0; ch2 != EOF; count = 0) {
            ch = getc(fp_in);   // grab first byte
            ch2 = getc(fp_in);  // grab second byte
            // write the bytes
            do {
                putc(ch2, fp_out);
                count++;
            } while (count < ch);
        }
        fclose(fp_in);
        fclose(fp_out);
        fprintf(stderr, "File Decompressed\n");
    }
4

2 回答 2

6

你错过了检查你的运行长度计数是否有字符溢出,所以如果你的文件中有超过 255 个相同的字符,这将出错。

于 2014-06-18T10:03:00.740 回答
1

这是工作源代码:

    // Chapter 22 Programming Project #7

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>

    void compress_file(FILE *fp_in, FILE *fp_out);
    void uncompress_file(FILE *fp_in, FILE *fp_out);

    int main(void)
    {
        FILE *fp_in, *fp_out;
        char nm_in[FILENAME_MAX], nm_out[FILENAME_MAX];
        int chk;

        for (;;) {
            printf(" ----------------------------------------- \n");
            printf("|             1 - Compress                |\n");
            printf("|             2 - Decompress              |\n");
            printf("|             3 - Exit                    |\n");
            printf(" ----------------------------------------- \n");
            do {
                printf("Enter a command: ");
                scanf(" %d", &chk);
            } while (isalpha(chk));

            if (chk == 3)
                exit(EXIT_SUCCESS);

            printf("Enter input file name: ");
            scanf(" %s", nm_in);
            printf("Enter output file name: ");
            scanf(" %s", nm_out);
            // Open file to read from
            while ((fp_in = fopen(nm_in, "rb")) == NULL) {
                fprintf(stderr, "Can't open \"%s\"\n", nm_in);
                printf("Enter input file name: ");
                scanf(" %s", nm_in);
            }
            // Open file to write to
            while ((fp_out = fopen(nm_out, "wb")) == NULL) {
                fprintf(stderr, "Can't create \"%s\"\n", nm_out);
                printf("Enter output file name: ");
                scanf(" %s", nm_out);
            }
            switch(chk) {
                case 1: compress_file(fp_in, fp_out); break;
                case 2: uncompress_file(fp_in, fp_out); break;
            }
            putchar('\n');
        }

        return 0;
    }

    void compress_file(FILE *fp_in, FILE *fp_out)
    {
        int count, ch, ch2, chk;

        ch = getc(fp_in);
        ch2 = ch;
        while (ch2 != EOF) {
            // if next byte is the same increase count and test
            for (count = 0; ch2 == ch && count < 255; count++) {
                ch2 = getc(fp_in); // set next variable for comparison
            }
            // write bytes into 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;

        for (count = 0; ch2 != EOF; count = 0) {
            ch = getc(fp_in);   // grab first byte
            ch2 = getc(fp_in);  // grab second byte
            // write the bytes
            do {
                putc(ch2, fp_out);
                count++;
            } while (count < ch);
        }
        fclose(fp_in);
        fclose(fp_out);
        fprintf(stderr, "File Decompressed\n");
    }
于 2014-06-18T16:20:28.993 回答