Gustavo Pinsard Gustavo Pinsard - 3 months ago 29
C++ Question

QFile.write( myStruct ) - how?

I'm beginning with Qt and am stuck with a problem supposedly for quite a long time now. I'm sure it's just something I don't see in C++. Anyway, please look at the following simple code and point me what am I doing wrong:

typedef struct FILEHEADER {
char udfSignature[8];
char fileName[64];
long fileVersion;
UNIXTIME fileCreation;
UNIXTIME lastRebuild;
FILEPOINTER descriptor;
} fileheader;

QFile f("nanga.dat");
if(f.open(QIODevice::ReadWrite));

f.write(fileheader);


Qt 5.2.0 trows me the following error message:

C:\sw\udb\udb\main.h:113: error: no matching function for call to
'QFile::write(FILEHEADER&)'
file.write(header);
^


Any suggestion on how I can write this struct to a
QFile
?

Thanks

Answer

QFile has write method which accepts arbitrary array of bytes. You can try something like this:

fileheader fh = { ...... };
QFile f("nanga.dat");
if(f.open(QIODevice::ReadWrite))
    f.write(reinterpret_cast<char*>(&fh), sizeof(fh));

But remember that in general, it's not a good idea to store any data this way.