Unsolved QDataStream question
-
For reading the size i must to use qdatastream i think other way is not possible. But i'm not sure what does qdatastream add me the binary data at the first of my file and then read it¿?
-
Here's an example. Hope it works :-)
#include <QGuiApplication> #include <QDataStream> #include <QDebug> #include <QFile> void write_bytearray_to_file() { const QByteArray ba("Hello world"); QFile file("file.bin"); file.open(QIODevice::WriteOnly); QDataStream out(&file); out << ba.size(); out.writeRawData( ba.constData(), ba.size() ); } void read_bytearray_from_file() { QFile file("file.bin"); file.open(QIODevice::ReadOnly); QDataStream in(&file); int ba_size = 0; QByteArray ba; in >> ba_size; ba.resize(ba_size); in.readRawData(ba.data(), ba_size); qDebug() << ba_size; qDebug() << ba; } int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); write_bytearray_to_file(); read_bytearray_from_file(); return EXIT_SUCCESS; }
-
Here is a slightly different version, using
writeBytes
andreadBytes
:void write_bytearray_to_file() { const QByteArray ba("Hello world"); QFile file("file.bin"); file.open(QIODevice::WriteOnly); QDataStream out(&file); out.writeBytes( ba.constData(), ba.size() ); } void read_bytearray_from_file() { QFile file("file.bin"); file.open(QIODevice::ReadOnly); QDataStream in(&file); uint ba_size = 0; char *c = nullptr; in.readBytes(c, ba_size); QByteArray ba(c, ba_size); delete [] c; qDebug() << ba_size; qDebug() << ba; }
-
it's frequent this error?
1297780736 QIODevice::read (QFile, "\Desktop\visual\build-binder-Qt_5_7_0_msvc2 013_static-Debug\debug\sha.exe"): maxSize argument exceeds QByteArray size limit ASSERT failure in QList<T>::operator[]: "index out of range", file C:\Qt\Qt5.7.0 \5.7\mingw53_32\include/QtCore/qlist.h, line 545 This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
I was trying like you teach me write the size and write data. And then read this size and extract my data.
-
Hi
You should qDebug() the values when you write them and when you read them in again.It sounds like you read something wrong and use for size
"maxSize argument exceeds QByteArray size limit" -
@mrjj i think i cant include binary data in pe format not sure to be read. I could use other file to get my trama but i dont know if it is good idea i think not because i will write my trama in this file and to read my trama again read the other file to get my trama or size of my trama. I'm saying like external file. But i think it's very ugly to do that. I could delimiter my trama and extract it but i think is difficult to do. Anyways, i dont know what is the maximum length of one qstring ¿? not sure.
-
@RIVOPICO Max length is 2 GB
-
Ps. If you reading PE exe file, you should NOT ever put the input in a string.
It must be bytearray at all times if ever to be an exe file again. :) -
yeah i can add qbytearray but for reading it? i must to know the size. or use bin or .dat or other file.
-
@RIVOPICO
Hi
To know size later. u must store it yourself.
Size|bytearray
Then you can know when reading it again.