c++ - 处理霍夫曼压缩/解压缩中的最后一个字节
问题描述
我有一个程序可以根据在文本输入文件中读取的 ASCII 字符频率生成 Huffman 树。霍夫曼代码存储在 256 个元素的字符串数组中,如果字符未被读取,则为空字符串。然后,该程序还对输出文件进行编码和压缩,然后能够将压缩文件作为输入文件并进行解压缩和解码。
总之,我的程序采用输入文件压缩和编码输出文件,关闭输出文件并打开编码作为输入文件,并采用一个新的输出文件,该文件应该具有与原始文本输入文件相同的解码消息.
我目前对这个程序的问题:解码压缩文件时,我得到一个额外的字符,这不在解码的原始输入文件中。这是由于我所知道的垃圾位。通过研究,我发现一种解决方案可能是在读取垃圾位之前使用伪 EOF 字符停止解码,但我不确定如何在我当前处理编码和解码的函数中实现这一点,因此非常感谢所有指导和帮助.
我的最终目标是能够使用这个程序来完全解码编码文件,而不会将垃圾位发送到输出文件。
下面我有两个函数,encodedOutput 和 decodeOutput 处理压缩和解压缩。
(对于 encodedOutput 函数,fileName 是输入文件参数,fileName2 是输出文件参数)
(对于decodeOutput函数,fileName2是输入文件参数,fileName 3是输出文件参数)
code[256] 是这两个函数的参数,保存原始输入文件中读取的每个唯一字符的 Huffman 代码,例如,在输入文件中读取的字符“H”可能具有代码“111”在将其传递给函数时,存储在 code[72] 的代码数组中。
freq[256] 保存每个 ascii 字符的读取频率,如果它不在原始输入文件中,则保存 0。
void encodeOutput(const string & fileName, const string & fileName2, string code[256]) {
ifstream ifile; //to read file
ifile.open(fileName, ios::binary);
if (!ifile)//to check if file is open or not
{
die("Can't read again"); // function that exits program if can't open
}
ofstream ofile;
ofile.open(fileName2, ios::binary);
if (!ofile) {
die("Can't open encoding output file");
}
int read;
read = ifile.get(); //read one char from file and store it in int
char buffer = 0, bit_count = 0;
while (read != -1) {//run this loop until reached to end of file(-1)
for (unsigned b = 0; b < code[read].size(); b++) { // loop through bits (code[read] outputs huffman code)
buffer <<= 1;
buffer |= code[read][b] != '0';
bit_count++;
if (bit_count == 8) {
ofile << buffer;
buffer = 0;
bit_count = 0;
}
}
read = ifile.get();
}
if (bit_count != 0)
ofile << char(buffer << (8 - bit_count));
ifile.close();
ofile.close();
}
void decodeOutput(const string & fileName2, const string & fileName3, string code[256], const unsigned long long freq[256]) {
ifstream ifile;
ifile.open(fileName2, ios::binary);
if (!ifile)
{
die("Can't read again");
}
ofstream ofile;
ofile.open(fileName3, ios::binary);
if (!ofile) {
die("Can't open encoding output file");
}
priority_queue < node > q;
for (unsigned i = 0; i < 256; i++) {
if (freq[i] == 0) {
code[i] = "";
}
}
for (unsigned i = 0; i < 256; i++)
if (freq[i])
q.push(node(unsigned(i), freq[i]));
if (q.size() < 1) {
die("no data");
}
while (q.size() > 1) {
node *child0 = new node(q.top());
q.pop();
node *child1 = new node(q.top());
q.pop();
q.push(node(child0, child1));
} // created the tree
string answer = "";
const node * temp = &q.top(); // root
for (int c; (c = ifile.get()) != EOF;) {
for (unsigned p = 8; p--;) { //reading 8 bits at a time
if ((c >> p & 1) == '0') { // if bit is a 0
temp = temp->child0; // go left
}
else { // if bit is a 1
temp = temp->child1; // go right
}
if (temp->child0 == NULL && temp->child1 == NULL) // leaf node
{
answer += temp->value;
temp = &q.top();
}
}
}
ofile << ans;
}
解决方案
将其更改为freq[257]
和code[257]
,并设置freq[256]
为 1。您的 EOF 是符号 256,它会在流的末尾出现一次。在编码结束时,发送符号 256。当您在解码时收到符号 256 时,停止。
推荐阅读
- javascript - 虽然循环导致冻结问题....任何替代循环方法?
- sql-server - 我从 VBA 调用存储过程,然后更新数据。更新时出现错误。我该如何解决?
- python - 模块“psycopg2.errors”没有“UndefinedColumn”成员
- xml - 当标签中没有元素而不是另一个标签时,在 Hive 中解析 XML 数据
- python - 给定一个 numpy 整数数组,我想在该数组中返回一个随机序列?
- go - Golang SHA512 不匹配 OpenSSL SHA512
- flutter - GetX 状态管理
- jestjs - 运行 Jest 测试时使用 jsForce 登录 Salesforce
- code-editor - 在 VMware 中安装 DOMJudge
- ios - NEVPNManager 停止使用 iOS 14 beta