PDA

View Full Version : Write QBitArray to file with QDataStream



vdsf
9th September 2011, 09:51
Hi, I'm trying to write a QBitArray to file using a QDataStream but I think I may have the syntax wrong.

This is an example of how I'm doing it.



QBitArray test1;
test1.resize(test1.size()+ 4);
test1.setBit(0, true);
test1.setBit(1, false);
test1.setBit(2, true);
test1.setBit(3, false);

QFile file ("testFile.data");
file.open(QIODevice::WriteOnly);

QDataStream out(&file);

out << test1;

file.close();

wysota
9th September 2011, 10:20
I don't know what you mean that you want to "see binary" ("Matrix"?). But since you are using QDataStream you will probably get something else than you want.

vdsf
9th September 2011, 11:29
Ok maybe I should elaborate a bit more. I'm writing a huffman encoding program to compress a text file. The problem I'm having is that my encoded files are coming out bigger than the original text input file, which led me to believe that my syntax was wrong.

Also I think I took the term binary file too literally which is why I expected to see 0's and 1's when I opened it.

Here is the code for my encoding function.



void MainWindow::huffEncode(QMap<QChar, QBitArray> codeMap)
{
QFile file("file1.huff");
file.open(QIODevice::WriteOnly);
QDataStream out (&file);

for (int i = 0; i < usrText.length(); i++) // Iterate over user input string
{
QChar symbol = usrText[i]; // each symbol in string

QMap<QChar, QBitArray>::iterator j;
for (j = codeMap.begin(); j != codeMap.end(); j++) // iterate over codeMap (QMap of characters to huffman codes)
{
// comparison?

if (symbol == j.key()) // if the symbol is equal to the key of the codeMap
{

out << j.value(); //add the huffman code of the character to the file

}

}

}
file.close();

}

wysota
9th September 2011, 11:46
But why are you using QDataStream? Do you know what it does? It is not a transparent general purpose binary streamer but rather a serialization mechanism. Furthermore, you can't write incomplete bytes into a file. There is no way to write four bits into a file, you need to write a whole byte, no filesystem I know of allows files with sizes of fractions of a byte.

vdsf
9th September 2011, 12:25
The reason I'm using it is because my lecturer told me to. He also said that QDataStream would automatically pad bits for QBitArray in order to make up a whole byte.

DanH
9th September 2011, 12:42
To write "raw" data to a QDataStream you must use "writeRawData". Otherwise, the data written is only readable by another instance of QDataStream.

wysota
9th September 2011, 13:29
The reason I'm using it is because my lecturer told me to.
Ask him why he did that then. It's not a correct approach.


He also said that QDataStream would automatically pad bits for QBitArray in order to make up a whole byte.
Doesn't it defeat the purpose of using huffman code? Besides, your lecturer is wrong.


QDataStream &operator<<(QDataStream &out, const QBitArray &ba)
{
quint32 len = ba.size();
out << len;
if (len > 0)
out.writeRawData(ba.d.constData() + 1, ba.d.size() - 1);
return out;
}
If you write a four bit array to data stream, 40 bits will be written to the device. And no padding is there, actually. The superflous bits are just ignored, from what I see.