簡體   English   中英

在霍夫曼壓縮/解壓縮中處理最后一個字節

[英]Handling last byte in huffman compression/decompression

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

總而言之,我的程序將一個輸入文件壓縮並編碼為一個輸出文件,關閉該輸出文件並以輸入文件的形式打開編碼,並獲取一個新的輸出文件,該文件應具有與原始文本輸入文件相同的解碼消息。 。

我目前與該程序有關的問題:解碼壓縮文件時,我得到一個額外的字符,或者該字符不在解碼的原始輸入文件中。 這是由於我所知道的垃圾位。 通過研究,我發現一種解決方案可能是使用psuedo-EOF字符在讀取垃圾位之前停止解碼,但是我不確定如何在當前處理編碼和解碼的函數中實現此功能,因此非常感謝所有指導和幫助。

我的最終目標是能夠使用此程序來完全解碼編碼的文件,而無需將垃圾位發送到輸出文件。

下面我有兩個函數,encodedOutput和decodeOutput處理壓縮和解壓縮。

(對於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; //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時,請停止。

暫無
暫無

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

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