0

I'm working on huffman encode. I don't know how should I save code. I can't find and saving variables bytes. I found all I need. Now starting my problem. I want to write data file (encode).

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();
}

This is my write function. Problem is here.

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();
}
borisbn
  • 4,988
  • 25
  • 42
seniorc
  • 69
  • 1
  • 2
  • 6
  • What exactly isn't working? What do you expect it to do? What is it actually doing that's incorrect? – Eric Dec 15 '13 at 18:34
  • void encode::writedata() function problem.numberMap=4,i must write byte numberMap to file and for example a frequency=70 so a code is "001" how can i write frequency and code bytes to file. – seniorc Dec 15 '13 at 18:42
  • I'm afraid I'm having some difficulty understanding. What exactly is happening that is incorrect? Are there error messages? What are the results? – Eric Dec 15 '13 at 18:47
  • i hove no error message.simply my problem is i have the all i needs information(freq,code...) but i dont know how can i write format(order,type) them file. – seniorc Dec 15 '13 at 18:58
  • Store the bytes (or bit sequences of variable length?) to a QByteArray, then when complete, write the QByteArray to file with QFile, do not use QTextStream or QDataStream. Much easier to get it to work when you have separate encoding step, independent of file IO. – hyde Dec 15 '13 at 19:07
  • can u give me simple example? For example i have int and QString how can we write QByteArray and how can QByteArray write file bytes – seniorc Dec 15 '13 at 19:15

0 Answers0