我有一个问题,当我在41 kb文件上使用它时它被压缩(虽然因为它使用行程编码,它似乎总是加倍文件大小)并正确解压缩。但是,当我尝试在一个16,173 kb的文件上使用它并对其进行解压缩时,它没有打开,文件大小为16,171 kb ....所以它解压缩但它没有返回到它& #39;原始形式....搞砸了......让我感到困惑,我似乎无法弄清楚我做错了什么......
使用的方法是行程编码,它用计数后跟字节替换每个字节。
在:
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");
}
答案 0 :(得分:6)
您错过了检查字符溢出的运行长度计数,因此如果您的文件中依次包含超过255个相同字符,则会出错。
答案 1 :(得分:0)
以下是工作源代码:
// 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");
}