1
votes

if you look in the documentation of QT. You can use readBytes or readRawBytes to read the binary data from a file. I am comfortable in any case, either reading the data from file or stream. In case of readBytes - Reads the buffer s from the stream and returns a reference to the stream. In case of readRawBytes - Reads L (length) bytes from the stream into s(buffer char*) and returns the number of bytes reads.

void readBinary(QString path)
{
    QFile file(path);
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << "Could not open bin file for reading";
        return;
    }

    QDataStream in(&file);
    char *data = new char[30];

    qint32 bytes = in.readRawData(data, 30);
    //in >> data;
    qInfo() << "bytes read: " << bytes;
    qInfo() << data;

    file.close();
}

It shows no of bytes reads but not showing binary data on a screen.

  1. What I am missing here?
  2. Do we need to do serialization/de-serialization of binary data? In other words marshelling/un-marshelling of data. because I have read in official documentation encoding/decoding you need to take care of by own and need to set version of QT while reading/writing the data in the file.
  3. How do we write back to the file/stream.
  4. If we have another method to read/write the data directly from a file.
  5. Do we need to write the whole binary data into buffer and then reads it again? This way we can maintain format of data. Want some answers from you guys!

for your reference - consider the snippet of binary data in the file as mentioned below.

00000000: 0000 0520 a52a 0108 8520 0108 9320 0108  ... .*... ... ..
00000010: 9920 0108 9f20 0108 a520 0108 0000 0000  . ... ... ......

Links which I had followed on StackOverflow to resolve this issue are- Link1 , Link2, Link3, Link4

1
You're treating data as a null terminated char array which it (probably) isn't. Try reading the data into a QByteArray and printing that.G.M.
@G.M. readRawData basically takes input as char *. Conversion from QByteArray to char * is not possible.shashank arora

1 Answers

1
votes
  1. What I am missing here?

You are using raw pointers instead of QByteArray. And then you are trying to print binary data as a text.

...
QDataStream in(&file);

// You can use QByteArray which was created especially ti avoid raw char pointers
QByteArray ba(30, 0);

qint32 bytes = in.readRawData(ba.data(), ba.size());

qInfo() << "bytes read: " << bytes;
qInfo() << ba.toHex('\s'); // You read binary data, not a text. Do not try to print it as a text
  1. Do we need to do serialization/de-serialization of binary data? In other words marshelling/un-marshelling of data. because I have read in official documentation encoding/decoding you need to take care of by own and need to set version of QT while reading/writing the data in the file.

Generally speaking, yes. Anyway you will have some serialization/de-serialization mechanism in your code. Qt suggest the mechanism out-of-the-box. You can forget about raw pointers, sizes, byte orders.

From the docs:

You don't have to set a version if you are using the current version of Qt, but for your own custom binary formats we recommend that you do; see Versioning in the Detailed Description.

QByteArray ba;
// fill it by some data

// Write to file
QDataStream out(&file); // output file, socket, etc.
out << ba;

// Read from file
QDataStream in(&file);
in >> ba;
  1. How do we write back to the file/stream.
// Write to file
QDataStream out(&file); // output file, socket, etc.
out << ba;
  1. If we have another method to read/write the data directly from a file.
  2. Do we need to write the whole binary data into buffer and then reads it again? This way we can maintain format of data. Want some answers from you guys!

With QDataStream you can read/write directly the data structures you need, for example:

struct MyStruct
{
    double d;
    int a;
    QString str;
    QPixmap pix;
    QVector<int> vec;    

    //overload the operators
    friend QDataStream &operator << (QDataStream &out, const MyStruct &d)
    {
        out << d.d << d.a << d.str << d.pix << d.vec;
        return out;
    }

    friend QDataStream &operator >> (QDataStream &in, MyStruct &d)
    {
        in >> d.d >> d.a >> d.str >> d.pix >> d.vec;
        return in;
    }    
}

// Now you can:
MyStruct data;

out << data;