PDA

View Full Version : Binary QFile error



Affenbrotbaum
28th January 2010, 21:02
Hey Community,

I am running the following piece of code on a Windows 7 machine and the output seems really strange. I get a string length of 0 back.
Am I missing something?

Thanks a lot
Alex



// Save the file
QFile fileOne("test.file");
fileOne.open(QIODevice::WriteOnly);
QDataStream out(&fileOne);
out.setVersion(QDataStream::Qt_4_6);
out << "Test";
fileOne.close();

//load from it
QFile file("test.file");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QString str;
in.setVersion(QDataStream::Qt_4_6);
in >> str ;

QString strLength;
strLength.setNum(str.length());

QMessageBox::information(0,"Output",str);
QMessageBox::information(0,"Output",strLength);
file.close();

schnitzel
28th January 2010, 21:34
from the documentation for QDataStream:


Each item written to the stream is written in a predefined binary format that varies depending on the item's type. Supported Qt types include QBrush, QColor, QDateTime, QFont, QPixmap, QString, QVariant and many others. For the complete list of all Qt types supporting data streaming see the Format of the QDataStream operators.


you are reading it out as a QString, so you must also write it in as a QString

you need to change:
out << "test"
to
out << QString("test")

boudie
28th January 2010, 21:42
Try:


out << QString("Test");


/edit:
I was too slow...

Affenbrotbaum
29th January 2010, 02:13
Argh.. could've seen that one myself.

Thanks you two...