QFile.write(myStruct)-怎么样? [英] QFile.write( myStruct ) - how?
问题描述
我从Qt开始,并且很长一段时间以来一直陷入一个问题.我确定这只是我在C ++中看不到的东西.无论如何,请查看以下简单代码,并指出我在做什么错:
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向我抛出以下错误消息:
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);
^
关于如何将该结构写入QFile
的任何建议?
Any suggestion on how I can write this struct to a QFile
?
谢谢
推荐答案
QFile
具有 write 方法,该方法接受任意字节数组.您可以尝试这样的事情:
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.
这篇关于QFile.write(myStruct)-怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!