[英]Handling extra bytes in huffman compression/decompression
我有一個程序可以根據在文本輸入文件中讀取的ASCII字符頻率生成霍夫曼樹。 霍夫曼碼存儲在由256個元素組成的字符串數組中;如果未讀取字符,則為空字符串。 然后,該程序還會對輸出文件進行編碼和壓縮,並且目前在解壓縮和解碼方面具有某些功能。
總而言之,我的程序將一個輸入文件壓縮並編碼為一個輸出文件,關閉該輸出文件並以輸入文件的形式打開編碼,並獲取一個新的輸出文件,該文件應具有與原始文本輸入文件相同的解碼消息。 。
我的問題是,在壓縮時的測試運行中,我注意到我有3個額外的字節,而當我對編碼文件進行解壓縮和解碼時,這3個額外的字節將被解碼為我的輸出文件。 根據原始輸入文件中文本的數量,我的其他測試將輸出這些額外的字節。
我的研究讓我提出了一些建議,例如將編碼輸出文件的前8個字節設置為無符號長long的64位,以給出文件中的字節數,或者使用psuedo-EOF,但我仍然堅持如何我會去處理它,考慮到我已經編寫的代碼,這兩種方法中哪一種是處理它的明智方法?
對此問題的任何指導或解決方案均不勝感激。
(對於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 << (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;
}
由於有完整的提升規則, (buffer << (8 - bit_count))
將是一個整數表達式,導致要寫入4個字節。 要只寫一個字節,需要將其強制轉換為char。
ofile << char(buffer << (8 - bit_count));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.