霍夫曼解码函数重复解压缩一个字符

问题描述 投票:0回答:1

我有一个程序,它根据在文本输入文件中读取的ASCII字符频率生成一个霍夫曼树。霍夫曼代码存储在256个元素的字符串数组中,如果未读取字符则为空字符串。该程序还编码和压缩输出文件。

我现在正在尝试解压缩和解码当前输出文件,该文件作为输入文件打开,新输出文件将解码消息与原始文本输入文件相同。

我对这部分任务的思考过程是重新创建一个带有霍夫曼代码的树,然后一次读取8位,遍历树直到我到达叶节点,我将更新一个空字符串(字符串答案)然后将其输出到我的输出文件。

我的问题:写完这个函数后,我看到原始输入文件的所有其他字符之间只有一个字符重复输出。我很困惑为什么会这样,因为我期望输出文件与原始输入文件相同。

对此问题的任何指导或解决方案表示赞赏。

(对于encodedOutput函数,fileName是输入文件参数,fileName2是输出文件参数)

(对于decodeOutput函数,fileName2是输入文件参数,fileName 3是输出文件参数)

code [256]是这两个函数的参数,并保存原始输入文件中读取的每个唯一字符的Huffman代码,例如,在输入文件中读取的字符“H”可能具有代码“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++ data-structures visual-studio-2017 huffman-code
1个回答
3
投票
(c >> p & 1) == '0'

只有当(c >> p & 1)等于48时才会返回true,因此你的if语句将始终跟随else分支。正确的代码是:

(c >> p & 1) == 0
© www.soinside.com 2019 - 2024. All rights reserved.