'Error when writing data to binary file c++ qt
I am making a c++ qt program and I need to write data to a binary file. But there's a problem. When I do this, extra empty bytes are written to the file. Here is my code:
QDataStream output(&fileOut);
QBitArray result;
result.resize(8);
result.fill(true);
output << result;
As a result, my file consists of 5 bytes instead of 1:
00000000
00000000
00000000
00001000
11111111
Does anyone know how to solve this problem?
Solution 1:[1]
Using QDataStream to write binary data in any form which should not be read again by QDataStream will not work since QDataStream adds some more bytes to be able to deserialize the data later on. QDataStream is not meant to write plain data (as can be seen in the documentation) - use QFile::write() for it.
Solution 2:[2]
There may be some under the hood conversion shenanigans w/ qBitArray being sent to a stream.
- Try converting qBitArray to qint8
- If that fails, fry converting qBitArray to qByteArray. Then maybe also to qint8
QDatastream resource

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | chehrlic |
| Solution 2 | Frebreeze |
