The documentation says that QDataStream writes data in system independent way, but it says nothing about QBuffer. I develop a program that saves data in a file like this:
QByteArray a;
QBuffer b(&a);
b.open(QIODevide::WriteOnly);
quint32 x = 1;
b.write((char*)&x, sizeof(x));
b.close();
QFile f(...);
f.open(QIODevide::WriteOnly);
f.write(a.constData(), a.size());
f.close();
, and i want this file can be read in any other OS (win, linux, Mac OS). Will this code work or i must use QDataStream instead?
The QBuffer documentation says :
ie it is only a
QByteArrayunderneath. On the other hand aQByteArrayis portable because as long as you see the data as an array of byte and write one byte at a time you are fine. Your code will work:When you say
Is your file used by your program only or will it be used by other applications in the system?
QDataStreamprovides nicer functions forI\Oand you may be still able to take advantage of it.It will be platform specific.
xrepresentation in memory depend on the endianess.It doesn’t occur in theQBuffer, but when you do :If you are on machines of different endianess, you will obtain different values for the resulting array by doing
Take a look at the source code of QDataStream operator