seniorc
25th December 2013, 01:36
Hello,writing codes here
//Writing size of map
uint16_t streamOffset = (sizeof(int)+sizeof(char))*FrekMap.size()+sizeof(u int16_t);
codefile.write(reinterpret_cast<char*>(&streamOffset),sizeof(uint16_t));
//Writing map
for (QMap<char,int>::Iterator it = FrekMap.begin(); it != FrekMap.end(); ++it) {
codefile.putChar(it.key());
codefile.write(reinterpret_cast<char*>(&(it.value())), sizeof(int));
}
//Writing lenght last byte
codefile.putChar(compressed.length()%8);
while(compressed.length()%8!=0)
compressed.push_back('0');
//Writing code
compressedData=compressed.constData();
for(int i = 0; i < compressed.size(); i++ )
{
if( compressedData[i] == ZeroBit )
k++;
else if( compressedData[i] == OneBit )
{
buff |= bits[k];
k++;
}
if( k == 8 )
{
codefile.write(reinterpret_cast<char *>(&buff),sizeof(unsigned char));
buff = 0;
k = 0;
}
}
Reading codes here
//read size of map
readData.read(reinterpret_cast<char*>(&sizeMap),sizeof(sizeMap));
//read map
for (int i=sizeof(uint16_t);i<sizeMap;i+=sizeof(int)+sizeof(char)) {
char charofmap;
int freq;
readData.getChar(reinterpret_cast<char*>(&charofmap));
readData.read(reinterpret_cast<char*>(&freq), sizeof(int));
FreqTable.insert(charofmap,freq);
}
//read last byte lenght
readData.getChar(reinterpret_cast<char*>(&lengthLast));
//read code to bit
while (!readData.atEnd()) {
unsigned char ch;
readData.getChar(reinterpret_cast<char*>(&ch));
codeLetter+=unschartoQString(ch);
}
I can't read true for writing code.Where is my problem?
//Writing size of map
uint16_t streamOffset = (sizeof(int)+sizeof(char))*FrekMap.size()+sizeof(u int16_t);
codefile.write(reinterpret_cast<char*>(&streamOffset),sizeof(uint16_t));
//Writing map
for (QMap<char,int>::Iterator it = FrekMap.begin(); it != FrekMap.end(); ++it) {
codefile.putChar(it.key());
codefile.write(reinterpret_cast<char*>(&(it.value())), sizeof(int));
}
//Writing lenght last byte
codefile.putChar(compressed.length()%8);
while(compressed.length()%8!=0)
compressed.push_back('0');
//Writing code
compressedData=compressed.constData();
for(int i = 0; i < compressed.size(); i++ )
{
if( compressedData[i] == ZeroBit )
k++;
else if( compressedData[i] == OneBit )
{
buff |= bits[k];
k++;
}
if( k == 8 )
{
codefile.write(reinterpret_cast<char *>(&buff),sizeof(unsigned char));
buff = 0;
k = 0;
}
}
Reading codes here
//read size of map
readData.read(reinterpret_cast<char*>(&sizeMap),sizeof(sizeMap));
//read map
for (int i=sizeof(uint16_t);i<sizeMap;i+=sizeof(int)+sizeof(char)) {
char charofmap;
int freq;
readData.getChar(reinterpret_cast<char*>(&charofmap));
readData.read(reinterpret_cast<char*>(&freq), sizeof(int));
FreqTable.insert(charofmap,freq);
}
//read last byte lenght
readData.getChar(reinterpret_cast<char*>(&lengthLast));
//read code to bit
while (!readData.atEnd()) {
unsigned char ch;
readData.getChar(reinterpret_cast<char*>(&ch));
codeLetter+=unschartoQString(ch);
}
I can't read true for writing code.Where is my problem?