PDA

View Full Version : writing and reading problem



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?

anda_skoa
25th December 2013, 10:43
I would suggest to test the invidual pieces.
First check that the size is written and read correctly, because there is no point in continuing if it is not.

Then check writing and reading a map with one entry, and so on.

Btw, why such a complicated way of writing the map size? If you just write the map size's value you can also more easily iterate over that range when reading, no?

Cheers,
_

ChrisW67
27th December 2013, 06:37
I can't read true for writing code.Where is my problem?
You have failed to explain what you are expecting to see, what you are seeing, why that is wrong, what you done to diagnose it etc.