我做错了什么?首先我阅读符号和频率。然后构建二叉树。好的。但是当我想将所有二进制代码文本写入缓冲文件以进行解压时,就会出现移位。
例如:
压缩缓冲文件(二进制代码):
10111110011011111001001000
解压缓冲文件(二进制代码):
10111110101101111110010000100100
当然我是用8位来写二进制代码的。但也有文物。您可以使用代码并用纸张检查压缩缓冲区文件 - 好吧。在解压缩缓冲文件时 - 错误;
部分代码:
union CODE {
unsigned char codeFoFile;
struct byte {
unsigned b1 : 1;
unsigned b2 : 1;
unsigned b3 : 1;
unsigned b4 : 1;
unsigned b5 : 1;
unsigned b6 : 1;
unsigned b7 : 1;
unsigned b8 : 1;
} byte;
};
//~~~~HOW I WRITE~~~~~//
fwrite(&countOfLetters, sizeof(int), 1, fileOutput);
fwrite(&fileBuffSize, sizeof(int), 1, fileOutput);
fwrite(&tail, sizeof(int), 1, fileOutput);
for (int i = 0; i < countOfLetters; i++) {
fwrite(&str[i].ch, sizeof(str[i].ch), 1, fileOutput);
fwrite(&str[i].freq, sizeof(str[i].freq), 1, fileOutput);
}
//---------------
union CODE code1;
int j = 0;
for (int i = 0; i < fileBuffSize - tail; i++) {
byteArr[j] = fgetc(fileBuff);
if (j == 7) {
code1.byte.b1 = byteArr[0] - '0';
code1.byte.b2 = byteArr[1] - '0';
code1.byte.b3 = byteArr[2] - '0';
code1.byte.b4 = byteArr[3] - '0';
code1.byte.b5 = byteArr[4] - '0';
code1.byte.b6 = byteArr[5] - '0';
code1.byte.b7 = byteArr[6] - '0';
code1.byte.b8 = byteArr[7] - '0';
fputc(code1.codeFoFile, fileOutput);
j = 0;
}
j++;
}
//work with tail
j = 0;
printf("%\n ");
for (int i = 0; i <= tail; i++) {
byteArr[j] = fgetc(fileBuff);
if (j == tail) {
code1.byte.b1 = byteArr[0] - '0';
code1.byte.b2 = byteArr[1] - '0';
code1.byte.b3 = byteArr[2] - '0';
code1.byte.b4 = byteArr[3] - '0';
code1.byte.b5 = byteArr[4] - '0';
code1.byte.b6 = byteArr[5] - '0';
code1.byte.b7 = byteArr[6] - '0';
code1.byte.b8 = byteArr[7] - '0';
fputc(code1.codeFoFile, fileOutput);
}
j++;
}
//~~~~HOW I READ~~~~~//
for (int i = 0; i < fileBuffSize + tail; i++) {
//charFile = fgetc(fileInput);
fread(&charFile, sizeof(char), 1, fileInput);
code1.codeFoFile = charFile;
//code1.codeFoFile = charFile;
if (charFile != NULL && charFile != 1) {
buff[0] = code1.byte.b1 + '0';
buff[1] = code1.byte.b2 + '0';
buff[2] = code1.byte.b3 + '0';
buff[3] = code1.byte.b4 + '0';
buff[4] = code1.byte.b5 + '0';
buff[5] = code1.byte.b6 + '0';
buff[6] = code1.byte.b7 + '0';
buff[7] = code1.byte.b8 + '0';
for (int i = 0; i < 8; i++)
printf("%c", buff[i]);
printf("\n");
fwrite(&buff, sizeof(buff), 1, buffFile);
}
charFile = NULL;
}
最佳答案
您的代码存在问题:
- 位字段的顺序由实现定义。您无法确定
codeFoFile
的哪一位对应于byte.b1
等。您的代码不可移植,并且对于您要实现的算法来说可能不正确。 printf("%\n ");
不正确。您的意思是printf("\n ");
吗?- 在写入循环中,您对 tail 的处理不正确:如果
tail == 0
,则不应执行任何操作,将byteArr
中的所有字节初始化为0
,将tail
字节读取到第一个tail
元素中,并存储到循环后的位中。 - 在读取循环中,您应该检查
fread
的返回值,以验证数据是否已正确读取到charFile
中。 - 我不明白为什么接下来要测试
if (charFile != NULL && charFile != 1)
。NULL
是一个空指针常量,它可能会扩展为0
,特别是如果您使用 C++ 编译器进行编译,但无论如何,0
和1
是可以由压缩算法生成的完全有效的二进制值。忽略它们是不正确的。 - 用俄语注释您的代码对全局的普通读者没有帮助。
关于c - 霍夫曼,解压错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35673885/