我做错了什么?首先,我阅读符号和频率。然后构建二叉树。好。但是当我想将所有二进制代码文本写入缓冲区文件时,为了解压缩,有一些变化。
例如:
压缩缓冲区文件(二进制代码):
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;
}
答案 0 :(得分:2)
您的代码存在问题:
codeFoFile
的哪个位对应byte.b1
等。您的代码不可移植,可能对您要实施的算法不正确。printf("%\n ");
不正确。你的意思是printf("\n ");
?tail == 0
,则不应执行任何操作,将byteArr
中的所有字节初始化为0
,读取tail
字节进入第一个tail
元素并存储到循环后的位中。fread
的返回值,以验证数据是否已正确读入charFile
。if (charFile != NULL && charFile != 1)
。 NULL
是一个空指针常量,它可以扩展为0
,尤其是在使用C ++编译器进行编译时,但无论如何,0
和1
都是完全有效的二进制文件可以由压缩算法产生的值。忽略它们是不正确的。