简体   繁体   中英

Qt: Write Struct to File

I'm struggling with writing a struct to a file via QFile in Qt.

typedef struct {
    uint32_t timestamp;
    uint32_t recordType;
    uint32_t payloadLength;
    char* payload;
} DataPacket;

The DataPacket struct is populated elsewhere:

QString dataString = "$GPGSA,M,3,03,23,22,19,17,01,09,31,12,,,,1.9,0.9,1.6*3D";

DataPacket p;
memset(&p, 0, sizeof(DataPacket));
p.timestamp = now();
p.recordType = 1;
QByteArray arr = dataString.toLatin1();
p.payloadLength = arr.count();
p.payload = arr.data();

// Write the data to a log file
QFile f;
f.setFileName(outputFile);
bool ok = f.open(QIODevice::WriteOnly);

if (ok) {
    f.write((char *)&p, sizeof(DataPacket));
    f.close();
}

When I inspect the outputFile log file, the $GPGSA... string is no where to be seen. If I then additionally write the payload field separately:

if (ok) {
    f.write((char *)&p, sizeof(DataPacket));
    f.write(p.payload, p.payloadLength);
    f.close();
}

The $GPGSA string is subsequently written correctly.

I'm missing something...

Another thing is that to keep Qt compatibility rename some native types, so I recommend using equivalent types, in addition to using QString directly instead of char * payload and payloadLength as shown below:

struct DataPacket{
    quint32 timestamp;
    quint32 recordType;
    QString payload;
};

Also when writing it is better to use QDataStream as shown below:

QDataStream &operator>>(QDataStream &in, DataPacket &p){
    in >> p.timestamp >> p.recordType >>p.payload;
    return in;
}

QDataStream &operator<<(QDataStream &out, const DataPacket &p){
    out << p.timestamp << p.recordType <<p.payload;
    return out;
}

The following example shows its use:

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    const QString fileName = "file.log";

    QString dataString = "$GPGSA,M,3,03,23,22,19,17,01,09,31,12,,,,1.9,0.9,1.6*3D";

    DataPacket pin{1000, 123, dataString};

    QFile fout(fileName);
    if(fout.open(QIODevice::WriteOnly)){
        QDataStream out(&fout);
        out<<pin;
    }
    fout.flush(); // or fout.close();

    DataPacket pout;
    QFile fin(fileName);
    if (fin.open(QIODevice::ReadOnly)) {
        QDataStream in(&fin);
        in>>pout;
        qDebug()<<pout.timestamp<<pout.recordType<<pout.payload;
    }
    return 0;
}

Output:

1000 123 "$GPGSA,M,3,03,23,22,19,17,01,09,31,12,,,,1.9,0.9,1.6*3D"

Note: QDataStream sets payloadLength indirectly because it knows the size of QString.

Much simpler:

assuming your structure 'DataPacket'

void MyClass::write()
{
QFile out(FILE_PATH_NAME);
out.open(QIODevice::WriteOnly);
out.write((const char *)DataPacket,sizeof(DataPacket));
out.close();
}

void MyClass::read()
{
QFile in(FILE_PATH_NAME);
in.open(QIODevice::ReadOnly);
in.read((const char *)DataPacket,sizeof(DataPacket));
in.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM