我想将存储在矢量(保存结构)中的数据写入文件,并从该文件中读取数据。我尝试用QDataStream执行此操作并重载">>"和"<<&#;运营商。一切都工作,直到我添加一个char数组到结构。 我尝试从QDataStream中读取字符串的方式一定有问题。我尝试了不同的方法来读取字符串,但我总是收到错误。
以下是结构:
typedef struct
{
char data[cNetworkMessageLengthMax + 1]; //cNetworkMessageLengthMax=19
} __attribute__((packed)) networkMessageBody_t;
typedef struct
{
baseStation_daytime_t timestamp;
uint32_t blueId;
networkMessageBody_t message;
} __attribute__((packed)) baseStation_mailbox_t;
和运营商:
QDataStream &operator<<(QDataStream &out, const std::vector<baseStation_mailbox_t> &data)
{
for(uint8_t i=0; i < data.size(); i++)
{
out << data[i].timestamp.time.hour;
out << data[i].timestamp.time.minute;
out << data[i].timestamp.time.second;
out << data[i].timestamp.time.thousandth;
out << data[i].timestamp.date.day;
out << data[i].timestamp.date.month;
out << data[i].timestamp.date.year;
out << data[i].blueId;
out << data[i].message.data;
}
return out;
}
QDataStream &operator>>(QDataStream &in, std::vector<baseStation_mailbox_t> &data)
{
uint16_t tmp16;
uint32_t tmp32;
char tmpChar[20];
uint8_t i = 0;
while(in.atEnd() == false)
{
data.emplace_back();
in >> data[i].timestamp.time.hour;
in >> data[i].timestamp.time.minute;
in >> data[i].timestamp.time.second;
in >> tmp16;
data[i].timestamp.time.thousandth = tmp16;
in >> data[i].timestamp.date.day;
in >> data[i].timestamp.date.month;
in >> tmp16;
data[i].timestamp.date.year = tmp16;
in >> tmp32;
data[i].blueId = tmp32;
in >> tmpChar; //HERE I get errors
data[i].message.data = tmpChar;
i++;
}
return in;
}
答案 0 :(得分:2)
取代:
out << data[i].message.data;
与out << QByteArray::fromRawData(data[i].message.data,std::extent<decltyple(data[i].message.data)>::value);
char tmpChar[20];
与QByteArray tmpChar;
data[i].message.data = tmpChar;
与std::copy(tmpChar.constBegin(),tempChar.constEnd(),data[i].message.data);