1

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;
    }
2
  • 1
    Why not QString? Commented Jun 28, 2017 at 14:43
  • I cant change the char array data in networkMessageBody_t. Other than that I am not opposed to QString, but if I knew how to use it in this case I wouldnt be here ;) Commented Jun 29, 2017 at 7:09

1 Answer 1

2

replace:

  • out << data[i].message.data; with out << QByteArray::fromRawData(data[i].message.data,std::extent<decltyple(data[i].message.data)>::value);
  • char tmpChar[20]; with QByteArray tmpChar;
  • data[i].message.data = tmpChar; with std::copy(tmpChar.constBegin(),tempChar.constEnd(),data[i].message.data);
Sign up to request clarification or add additional context in comments.

2 Comments

That works, thank you! But even after looking at the manpage I don't really understand how that part is returning the size of the data array: std::extent<decltyple(data[i].message.data)>::value Could you elaborate what extent and value is doing here?
From en.cppreference.com/w/cpp/types/extent: template< class T, unsigned N = 0> struct extent; If T is an array type, provides the member constant value equal to the number of elements along the Nth dimension of the array You can replace it with 20 but this way if you change the size of data the size in extent is adjusted automatically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.