霍夫曼解码功能反复反复压缩一个字符
Huffman Decoding Function Uncompressing One Character Repeatedly
我有一个程序,该程序根据文本输入文件中的ASCII字符频率读取了Huffman树。Huffman代码存储在256个元素的字符串阵列中,如果字符未读取,则将其空字符串存储在空字符串中。该程序还编码和压缩输出文件。
我现在正在尝试解压缩和解码我的当前输出文件,该文件以输入文件打开,新的输出文件将使解码的消息与原始文本输入文件相同。
我在这部分作业的思考过程是用霍夫曼代码重新创建树,然后一次读取8位,穿过树,直到我到达叶子节点,我将更新一个空字符串(字符串答案)然后输出到我的输出文件。
我的问题:编写此功能后,我看到我原始输入文件的所有其他字符之间只有一个字符重复输出。我为此感到困惑,因为我希望输出文件与原始输入文件相同。
对此问题的任何指导或解决方案都将不胜感激。
(对于编码函数,文件名是输入文件参数,fileName2是输出文件参数)
(对于DecoDeOutput函数,文件名是输入文件参数,文件名3是输出文件参数)
代码[256]是这两个功能的参数" 111"存储在代码[72]时,将其传递给函数。
freq [256]保持每个ASCII字符的频率读取或保持0,如果它不在原始输入文件中。
void encodeOutput(const string & fileName, const string & fileName2, string code[256]) {
ifstream ifile;
ifile.open(fileName, ios::binary);
if (!ifile)
{
die("Can't read again");
}
ofstream ofile;
ofile.open(fileName2, ios::binary);
if (!ofile) {
die("Can't open encoding output file");
}
int read;
read = ifile.get();
char buffer = 0, bit_count = 0;
while (read != -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 << (buffer << (8 - bit_count));
ifile.close();
ofile.close();
}
// Work in progress
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
{
ans += temp->value;
temp = &q.top();
}
ofile << ans;
}
}
}
(c >> p & 1) == '0'
只有当(c >> p & 1)
等于48时,才会返回true,因此您的IF语句将始终遵循Else Branch。正确的代码是:
(c >> p & 1) == 0
相关文章:
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 如何检查一个c++字符串中有多少相同的字符/数字
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 关于字符数组,我正在尝试将数组中的每个字符分配给另一个值
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 使用动态分配将 char* 复制到另一个字符**
- 将字符向量复制到另一个向量
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 如何在不知道对应关系的情况下在字符串中搜索字符并将其分配给另一个字符?
- 计算在同一位置至少包含一个常用字符的不同字符串对
- 如果我们在其中输入一个整数,则字符会给出整数作为输出,但是当分配给它一个整数时,这不会发生。为什么?
- 如何检查一个字符是否与字符数组中的另一个字符匹配?
- C++:使用另一个字符将一个字符大写
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- C ++读取用户输入的字符一个一个符号
- 遍历数组或从打开的文件中获取字符 - 一个比另一个有什么优势
- 将文本文件的内容一个字符一个字符地读入矢量,不跳过空白或新行
- 这是更快和更有效的,处理一个字符一个字符作为字符或流