处理Huffman压缩/减压中的最后一个字节



我有一个程序,该程序根据文本输入文件中的ASCII字符频率读取了Huffman树。Huffman代码存储在256个元素的字符串阵列中,如果字符未读取,则将其空字符串存储在空字符串中。然后,该程序还编码和压缩输出文件,然后能够将压缩文件作为输入文件将其采用并进行解压缩和解码。

总而言之,我的程序采取输入文件压缩并编码输出文件,关闭输出文件并打开编码为输入文件,并采用一个应该具有与原始消息相同的解码消息的新输出文件文本输入文件。

我目前在此程序中的问题:解码压缩文件时,我会得到一个额外的字符,或者在原始输入文件中没有解码。这是由于我所知道的垃圾位。通过研究,我发现一种解决方案可能是使用psoudo-eof角色在阅读垃圾位之前停止解码,但我不确定如何在处理和解码的当前功能中实现此功能,以便所有的指导和帮助。。

我的最终目标是能够使用此程序来完全解码编码的文件,而无需发送到输出文件的垃圾位。

下面我有两个函数,即处理压缩和解压缩的编码输出和解码。

(对于编码函数,文件名是输入文件参数,fileName2是输出文件参数)

(对于DecoDeOutput函数,文件名是输入文件参数,文件名3是输出文件参数)

代码[256]是这两个功能的参数" 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]设置为一个。您的EOF是符号256,最后一次出现在流中。在编码结束时,发送符号256。当您在解码时收到符号256时,请停止。

最新更新