gpt4 book ai didi

c++ - 如何从 QDataStream 中读取数组

转载 作者:行者123 更新时间:2023-11-30 03:30:42 29 4
gpt4 key购买 nike

我想将存储在 vector (保存结构)中的数据写入文件,并从该文件读取数据。我尝试使用 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;
}

最佳答案

替换:

  • 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);

关于c++ - 如何从 QDataStream 中读取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44805411/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com