我正在研究霍夫曼编码。我不知道应该如何保存代码。我找不到并保存变量字节。我找到了我需要的一切。现在开始我的问题。我想写数据文件(编码)。
void encode::getHuffmanTree(int *frek,QString *filetext){
QList<HuffmanTree*>Codes;
char character;
for(int i=0;i<=Char_Limit;i++)
if(frek[i]>0){//Karakter varsa
HuffmanTree *HuffmanCode=new HuffmanTree();
HuffmanCode->frekans=frek[i];
character=(char)i;
HuffmanCode->karakter=character;
Codes.append(HuffmanCode);
}
qSort(Codes.begin(),Codes.end(),sortingHuffmanTree);
while(Codes.count()>1){
HuffmanTree *node1(Codes[0]);
HuffmanTree *node2(Codes[1]);
HuffmanTree *newnode=new HuffmanTree();
newnode->frekans=node1->frekans+node2->frekans;
newnode->isLeaf=false;
newnode->left=node1;
newnode->right=node2;
newnode->karakter=node1->karakter+node2->karakter;
node1->parent=node2->parent=newnode;
Codes.append(newnode);
Codes.removeOne(node1);
Codes.removeOne(node2);
qSort(Codes.begin(),Codes.end(),sortingHuffmanTree);
}
Codes[0]->root=true;
getHuffmanCode(Codes[0]);
writedata(filetext);
Codes.clear();
}
这是我的写功能。问题在这里。
void encode::writedata(QString *filetext){
QMap<char,int>FrekMap;
QByteArray byteNum;
int numberMap;
for(int i=0;i<Char_Limit;i++){
if(frekList[i]>0)
FrekMap.insert(char(i),frekList[i]);
}
delete[] frekList;
numberMap=FrekMap.keys().count();
QFile writedata(ui->FileLoca->text()+"t");
if(!writedata.open(QIODevice::WriteOnly)){
ShowMessage("File Error.Could not open...");
return;
}
QTextStream out(&writedata);
out.setVersion(QDataStream::Qt_4_8);
out<<numberMap;//number of FrekMap.key
/* writing freq table */
foreach(char sembol,FrekMap.keys()){
out<<sembol<<FrekMap.value(sembol);
}
/* writing encoding codes */
for(int i=0;i<=filetext->size();i++){
QString ch(filetext[0][i]);
out<<CodeHash.value(ch);
}
writedata.flush();
writedata.close();
}