Nominate our 2022 Qt Champions!

How to read .bin file and then split the QByteArray for the different variables

  • Hi,

    Let's say I have @class Person
    QString name;
    int age;
    double smthng;

    First for writing the data in the file I converted all variables to QByteArray and used QFile::write();

    Now I want to read the file but how am I suppost to take the different variables? If I use readall(); I will have one QByteArray but how to split it to know which byte for which variable is? Should I use sizeof or something like that?

  • Ok here is what I did for a solution.

    Since I have several QStrings I made one big QString that appends all small QStrings and between them there is a special sign (for example '$'). All the numbers I converted to QString also with QString::number().

    All the information is in one big QString so when I load it and use QFile::readAll() and then QByteArray::toHex and then QByteArray::toLatin1() I will have my big QString that I know how it's made.

    I will test now hope this plan works.

Log in to reply