QDataStream not writing
-
hi may you please assist the following code does not work it only ceate and empty file.dat file.
am using Qt 5.15.2 on windows 10 home 64 bit.#include <QCoreApplication>
#include <QFile>
#include <QImage>
#include <QDataStream>
#include <QDebug>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QFile file("file.dat"); file.open(QIODevice::WriteOnly); QDataStream out(&file); // we will serialize the data into the file out << QString("the answer is "); // serialize a string out << (qint32)42; // serialize an integer QFile file2("file.dat"); file2.open(QIODevice::ReadOnly); QDataStream in(&file2); // read the data serialized from the file QString str; qint32 b; in >> str >> b; // extract "the answer is" and 42 qDebug()<< str << b; return a.exec();
}
-
You should close the file before trying to open it again.
-
thank you it worked
-
nothing written on bytearray can you please assist
QFile file("img.png");
file.open(QIODevice::ReadOnly);
QDataStream out(&file); // we will serialize the data into the file
QByteArray data;
out >> data;
file.close();
qDebug()<< data.size(); -
Hi,
You are not checking that the file opening is successful. The path being relative, it's likely failing.
-
@tinashe Why do you think a png file contains QDateStream encoded data?
-
I think its my misunderstanding the use if this class I was just looking for a class which can make my life easier in sending picture data using Qudp or QTCP . Thanks