QDataStream question



  • I have one question with qdatastream with qdatastream i'm including to my file binary data if i'm not wrong. For example if i include different .txt files in one and i include binary data with qdatastream i could read this binary data or i must to use some trick by me. The problem i'm having like anyone can see in my old post's to include one qbytearray in my file with information of my file to after extract the files that has for example(different txt files) But i always put a size to my qbytearray to be able to read it. But when i want to have a undefined size i can't know which size i have so i can't read my qbytearray because i will get list out range or qassert. etc. some suggestion or idea how can be solved this issue. sorry i'm novice!


  • Qt Champions 2016

    Hi
    Im not really sure what you are doing with those byte arrays.
    It sounds like you have some of various sizes and you
    save them in same file.

    The issue is then reading them in again
    as now you dont know where one starts and ends?

    Is this correctly understood?



  • yeah i have different files and one qbytearray to get these files (extract them). For example i create one qbytearray like:

    QByteArray trama(1024,'0');
    

    Now i know the size of my trama so i can seek this trama and with this trama i know the sizes of my files to extract them. But if my qbytearray need more space i dont know how i will read my qbytearray if i dont know the size. So i can't seek my trama so what you recommend me? more or less.


  • Moderators

    You need to save the size in the file along with the actual data. After that you can first read the size and then read that specific number of bytes into a byte array back again.



  • 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¿?


  • Moderators

    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;
    }
    

  • Moderators

    Here is a slightly different version, using writeBytes and readBytes:

    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.


  • Qt Champions 2016

    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.


  • Moderators

    @RIVOPICO Max length is 2 GB


  • Qt Champions 2016

    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.


  • Qt Champions 2016

    @RIVOPICO
    Hi
    To know size later. u must store it yourself.
    Size|bytearray
    Then you can know when reading it again.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.