Qt: Write Struct to File

前端 未结 2 1514
面向向阳花
面向向阳花 2021-01-27 11:09

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

typedef struct {
    uint32_t timestamp;
    uint32_t recordType;
             


        
2条回答
  •  囚心锁ツ
    2021-01-27 11:55

    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();
    }
    

提交回复
热议问题