read binary data from QDataStream with feedback
-
There is official example how to read binary data from a stream (from file). I want the same but with feedback: how to know that there was error while deserializing?
This is my example
void MainClass::read(){ my_struct_t obj; QFile file("file.dat"); file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file in.startTransaction(); in >> obj.a; // extract 3 and true if(in.commitTransaction()){ qDebug().noquote().nospace() << "read is ok"; }else{ qDebug().noquote().nospace() << "unknown error"; } qDebug().noquote().nospace() << "obj.a=" << obj.a; }
To know about deserializing errors, should I use in.commitTransaction() and in.commitTransaction()? Or it is for special interfaces like serial port/socket only?
-
What other error do you expect than there was not enough data available for deserialization?
-
What other error do you expect than there was not enough data available for deserialization?
@Christian-Ehrlicher said in read binary data from QDataStream with feedback:
What other error do you expect than there was not enough data available for deserialization?
I expect the only one type of error: if(data available % data of pack) {error();}. In human speech: not enough data in file or too much data in file is error. But how to now it happen?
I was thinking to check by
if(!(file.size() % __datasizeof(my_struct_t))) makeWarning();
But unfortunately I use GCC last version, but __datasizeof() is still unavailable... Am I right?
Well, I can calc struct size manually and check by
if(!(file.size() % magicConstant)) makeWarning();
But I want to avoid magicConstant..
-
@Christian-Ehrlicher said in read binary data from QDataStream with feedback:
What other error do you expect than there was not enough data available for deserialization?
I expect the only one type of error: if(data available % data of pack) {error();}. In human speech: not enough data in file or too much data in file is error. But how to now it happen?
I was thinking to check by
if(!(file.size() % __datasizeof(my_struct_t))) makeWarning();
But unfortunately I use GCC last version, but __datasizeof() is still unavailable... Am I right?
Well, I can calc struct size manually and check by
if(!(file.size() % magicConstant)) makeWarning();
But I want to avoid magicConstant..
@DungeonLords said in read binary data from QDataStream with feedback:
But how to now it happen?
QDS::commitTransaction() will return false and QDataStream::status() will tell you why.
-
Thanks. Can I drop startTransaction() and commitTransaction()? Use like this
void MainClass::read(){ my_struct_t obj; QFile file("file.dat"); file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file in >> obj.a; if(in.status() == QDataStream::Ok){ qDebug().noquote().nospace() << "read is ok"; }else{ qDebug().noquote().nospace() << "error code " << in.status(); } qDebug().noquote().nospace() << "obj.a=" << obj.a; }
-
Thanks. Can I drop startTransaction() and commitTransaction()? Use like this
void MainClass::read(){ my_struct_t obj; QFile file("file.dat"); file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file in >> obj.a; if(in.status() == QDataStream::Ok){ qDebug().noquote().nospace() << "read is ok"; }else{ qDebug().noquote().nospace() << "error code " << in.status(); } qDebug().noquote().nospace() << "obj.a=" << obj.a; }
@DungeonLords
Yes, because you reading from regular file. You would need it if you were reading from a streaming device instead.Using Read Transactions
When a data stream operates on an asynchronous device, the chunks of data can arrive at arbitrary points in time. The QDataStream class implements a transaction mechanism that provides the ability to read the data atomically with a series of stream operators