Qt World Summit: Register Today!

Writing raw data to a file using QFile

  • I have some data (uint8_t, uint32_t, float) that I want to write to a raw data file, without any extra information.

    What is the best way to do this? I've read the QDataStream appends every QByteArray with information like the size of the array. This is not what I want; I simply want a file that is only the bytes that I write, and will only be interpretable if someone knows the correct order and size of the data types being written.

    I have tried writing the the QFile directly, but that requires a const char * object. What is the correct way to convert all this data to a const char * without wasting any space?

    A side note that is not really related: I am encrypting this file, and I believe the write way to do this is to extend QFile and reimpliment the write() method to encrypt the data before writing it.

  • Do you require a specific endianness?

  • const char *QByteArray::constData() const

  • @Smeeth

    I have tried writing the the QFile directly, but that requires a const char * object.

    But you will use the following overload: http://doc.qt.io/qt-5/qiodevice.html#write-2

    qint64 QIODevice::write(const QByteArray &byteArray)

    From your specifications, you will want to write directly to the file IMHO. You have no desire to share your data with anything else, so you will write & read it with your own code. Endian-ness might still be an issue if you want your own program to be cross-platform, e.g. write on one platform and use your own program to read it back on another platform, if you want that ability.

  • @JonB Thank you, I didn't realize there was an overload for byte array.

    Say I have the following data:

    uint8_t status = getStatus();
    uint8_t channel = getChannel();
    float sourceVal1 = getSource1();
    float sourceVal2 = getSource2();
    uint32_t ticks = getTicks();

    What is the correct way to transform my data into a byte array? The old style cast and static_cast<char> don't seem to do that job (I get a warning "implicit conversion changes signedness", which means this would not work, correct?)

    Endianess is not an issue as the platform will not change.

  • @Smeeth
    I'm afraid I'm just the wrong to person to ask that! I'd go memcpy(char_bur, &status, sizeof(status)), or write a union or use a (unsigned char *) cast. (I wouldn't get a "implicit conversion changes signedness", you shouldn't be accessing the int as an int, only its address. Or more like static_cast<char *>&status.) And make a QByteArray from the char buffer/pointer.

    (I'd really just go fwrite(&status, sizeof(status), 1, file_pointer), and the whole thing would be done in one line).

    None of which doubtless is at all allowed/encouraged now. So hopefully a C++ expert will offer some C++ or Qt friendly ways to get those bytes out of the variables.... :)

  • Thank you for your help. I believe I saw someone else suggest using memcopy in another thread somewhere so I will pursue that route. Thanks!

  • @Smeeth
    Please don't say I encouraged you to use memcpy. I'll get thrown out of the forum.

  • @JonB I swear I heard it elsewhere! :)

  • const char* charbytes{reinterpret_cast<const char*>(original_data)};
    int size{static_cast<int>(sizeof(original_type))};
    QByteArray bytes{charbytes, size};

  • Works the other way, too:

    auto myStruct = reinterpret_cast<MyStruct*>(bytearray.data());
    // "The pointer remains valid as long as the byte array isn't reallocated or destroyed."

    The same structure in both ends is of course handy unless you need really variable data. In the latter case you have to create a complicated protocol and it would be better to use a higher level protocol.

  • If you want to write raw data, use raw C functions :)

    #include <cstdio>
    FILE * file;
    file = fopen ("MyFile.dat", "wb");
    fwrite (&status , 1, 1, file);
    fwrite (&channel , 1, 1, file);
    fwrite (&sourceVal1 , sizeof(float), 1, file);
    fwrite (&sourceVal2 , sizeof(float), 1, file);
    fwrite (&ticks , sizeof(ticks), 1, file);
    fclose (file);

  • @mpergand
    OMG! But that's what I said. I assumed you'd be shot for that here!
    To be fair, I assumed the OP would want to use the QFile class to do his writing. Isn't one of the points of Qt to use QFile etc. instead of the C/C++ stdio stuff?

  • Lifetime Qt Champion

    What about QFile::writeData()?

  • Moderators

    To get back to the original question, I think we derailed a bit with QByteArray

    @Smeeth said in Writing raw data to a file using QFile:

    I have some data (uint8_t, uint32_t, float) that I want to write to a raw data file, without any extra information.

    Never the less @aha_1980 is right,´qint64 QIODevice::write(const char *data, qint64 maxSize)´ is the way to go

    this, should do just fine, its untested however.

    QFile f (...);
    uint8_t var1;
    uint32_t var2;
    float var3;
    f.write(reinterpret_cast<const char *>(&var1), sizeof(uint8_t));
    f.write(reinterpret_cast<const char *>(&var2), sizeof(uint32_t ));
    f.write(reinterpret_cast<const char *>(&var3), sizeof(float ));

  • Lifetime Qt Champion

    Works fine :)

    template< class aType >
    qint64 write( QFile& file, aType var ) {
      qint64 toWrite = sizeof(decltype (var));
      qint64  written = file.write(reinterpret_cast<const char*>(&var), toWrite);
      if (written != toWrite) {
        qDebug () << "write error";
       qDebug () << "out: " << written;
      return written;
    template< class aType >
    qint64 read( QFile& file, aType &var ) {
      qint64 toRead = sizeof(decltype (var));
      qint64  read = file.read(reinterpret_cast<char*>(&var), toRead);
      if (toRead != read) {
        qDebug () << "read error";
      qDebug () << "in: " << read;
      return read;
     QFile file("e:/test.txt");
      if (!file.open(QFile::WriteOnly)) {
      uint8_t var1 = 10;
      uint32_t var2 = 20000;
      float var3 = 10.8;
      write(file, var1);
      write(file, var2);
      write(file, var3);
      var1 = 0; var2 = 0; var3 = 0;
      if (!file.open(QFile::ReadOnly)) {
      read(file, var1);
      read(file, var2);
      read(file, var3);
    qDebug() << " result = " << var1 << " " << var2 << " " << var3;

    out: 1
    out: 4
    out: 4
    in: 1
    in: 4
    in: 4
    result = 10 20000 10.8

    but file is fragile to platform change etc.

Log in to reply