簡體   English   中英

霍夫曼解碼功能反復解壓縮一個字符

[英]Huffman Decoding Function Uncompressing One Character Repeatedly

我有一個程序可以根據在文本輸入文件中讀取的ASCII字符頻率生成霍夫曼樹。 霍夫曼碼存儲在由256個元素組成的字符串數組中;如果未讀取字符,則為空字符串。 該程序還對輸出文件進行編碼和壓縮。

我現在正在嘗試解壓縮和解碼我當前的輸出文件,該文件作為輸入文件打開,而新的輸出文件將使解碼后的消息與原始文本輸入文件相同。

我對作業的這一部分的思考過程是用霍夫曼代碼重新創建一棵樹,然后一次讀取8位,遍歷樹直到到達葉節點,在那里我將更新一個空字符串(字符串答案),然后將其輸出到我的輸出文件。

我的問題:編寫此函數后,我發現原始輸入文件的所有其他字符之間只有一個字符被重復輸出。 對於這種情況,我感到困惑,因為我期望輸出文件與原始輸入文件相同。

對此問題的任何指導或解決方案均不勝感激。

(對於encodedOutput函數,fileName是輸入文件參數,fileName2是輸出文件參數)

(對於decodeOutput函數,fileName2是輸入文件參數,fileName 3是輸出文件參數)

code [256]是這兩個函數的參數,並保存原始輸入文件中讀取的每個唯一字符的霍夫曼代碼,例如,輸入文件中讀取的字符“ 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 >> p & 1) == '0'

僅在(c >> p & 1)等於48時返回true,因此您的if語句將始終跟隨else分支。 正確的代碼是:

(c >> p & 1) == 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM