0

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?

3
  • I don't use qt, but for standard C++, you have to use the stream's write function to specify the exact number of bytes to write, and not use <<. This is especially the case if you're writing the file as "binary", where you need total control of how many bytes are written. Commented Apr 29, 2022 at 16:25
  • What's your definition for fileOut? Commented Apr 29, 2022 at 19:41
  • It's not a problem since you are trying to write an array. Extra 4 bytes those are written at the beginning are used to store a length of array. If you want to avoid this, you should convert the data of your bit array to another data type, qint8 for example, which one is 1 byte length Commented May 4, 2022 at 20:35

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Do you really think so? A data stream is a binary stream of encoded information which is 100% independent of the host computer's operating system, CPU or byte order. For example, a data stream that is written by a PC under Windows can be read by a Sun SPARC running Solaris. So it's universal and do not mislead people
So it's universal and do not mislead people - yes, it's universal in the way that it can be read and write from/to QDataStream on different platforms. But it's data format is not specified so you can not read it with something without known the Qt internals.
You can read and write without any restrictions. You do not need to know how it is realized in Qt. For example QTcpSocket is realized on top of QDataStream, but it works with any socket
For example QTcpSocket is realized on top of QDataStream, - no, QTcpSocket does not use QDataStream at all - you can send your data encoded as a QDataStream through a QTcpSocket but you have to read it on the other side with QDataStream too.
For example QTcpSocket is realized on top of QDataStream, but it works with any socket. I should be more correct, i wanted to say, that QDataStream provides serialization of binary data to a QIODevice, while QIODevice provides both a common implementation and an abstract interface for devices that support reading and writing of blocks of data, such as QFile, QBuffer and QTcpSocket. So you are free to use QDataStream to write/read to/from any stream without worry how this data would be read on another end.
|
0

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 enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.