I want to write data stored in a vector (holding structs) to a file and also read data from that file. I try to do this with QDataStream and overloading the ">>" and "<<" operators. Everything worked until I added a char array to the struct. Something must be wrong in the way I try to read the string from the QDataStream. I tried different ways to read the string but I always get an error.
Here are the structs:
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;
and the operators:
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;
}
QString?