[Interest] reading from and writing to a file with QDataStream
Sina Dogru
sinadooru at gmail.com
Mon Mar 7 14:41:49 CET 2016
Hello,
I am trying to read from and write to a file. But to do this, I use
QByteArray to pass it between some of the modules. But seems like things
does not work like I predicted.
Simply, on module A, I write some informations to a QByteArray. Then I pass
it to module B which writes to a file, and I do it for a bunch of times.
But when I try to read from this file, something goes wrong.
Here is a simplified version of what am I trying to do.
#include <QFile>
#include <QDataStream>
#include <QByteArray>
#include <QDebug>
int main()
{
// module A creates a byte array
QByteArray bai;
QDataStream out(&bai, QIODevice::WriteOnly);
out << QString("A QString"); // writes some information on it
// passing bai to module A
// module A do something...
QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out2(&file); // and module A write those datas to a file.
out2 << bai.size();
out2 << bai;
// and module B starts
if (file.isOpen())
file.close();
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QString str;
int a; // extracts size information
in >> a >> str; // extracts QString
file.close();
qDebug() << str << a; // output: "\u0000\u0012A QString" 22
return 0;
}
I guess using QByteArray get things messed up. But then is there any advice
to pass the information to another function?
Thank you,
Sina
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160307/eab98623/attachment.html>
More information about the Interest
mailing list