PDA

View Full Version : QDataStream reading into QString



jakamph
15th February 2006, 15:29
I'm trying to learn how to use QDataStream for serializing my data and then read it out again later. However, I'm having a trouble with reading out a QString.

The code is just "Hello World" type of app that is just used to see how things work instead of doing anything actually useful.


QFile outFile( "output.txt" );
outFile.open( IO_WriteOnly );

QDataStream outStream( &outFile );

outStream << "The beginning of a series of numbers";

for (int i = 0; i < 50000; i++)
{

outStream << (Q_INT32) i;

}/* end loop through numbers */

double outFloat = 3.1516342315;

double* addressOfFloat = &outFloat;

outStream << outFloat;

outStream << "The end of the series of numbers. Have a nice day.";

outFile.close( );


Opening up the binary file, everything looks to be in there correctly. The strings show up along with the binary for all of the numbers.

Reading out, however, is a different issue.


QFile inFile( "output.txt" );
inFile.open( IO_ReadOnly );
QDataStream inStream( &inFile );

Q_INT32 data[50000];

QString string;

inStream >> string;

for (int i = 0; i < 50000; i++)
{

inStream >> data[i];

}

double doubleCheck;
inStream >> doubleCheck;

inStream >> string;

inFile.close();


When I take a look at the strings that were read in, it shows up as a string of ??'s instead of the string that I output. This occurs the same whether I'm on Sun or Windows. Am I forgetting some function call?

Thanks in advance for any help.

zlatko
15th February 2006, 16:06
what strings actually insteads?

Codepoet
15th February 2006, 16:12
You did not write a QString but instead a
const char *
This

outStream << QString("The beginning of a series of numbers");
should work. Or you could change the type you are trying to read out of the stream.

jakamph
15th February 2006, 16:21
You did not write a QString but instead a
const char *
This

outStream << QString("The beginning of a series of numbers");
should work. Or you could change the type you are trying to read out of the stream.

This worked just fine. It's interesting that they didn't mention this in the example that trolltech has in the 3.3 documentation for QDataStream.

http://doc.trolltech.com/3.3/qdatastream.html#details (QDataStream Docs)


Example (write binary data to a stream):

QFile file( "file.dat" );
file.open( IO_WriteOnly );
QDataStream stream( &file ); // we will serialize the data into the file
stream << "the answer is"; // serialize a string
stream << (Q_INT32)42; // serialize an integer


Example (read binary data from a stream):

QFile file( "file.dat" );
file.open( IO_ReadOnly );
QDataStream stream( &file ); // read the data serialized from the file
QString str;
Q_INT32 a;
stream >> str >> a; // extract "the answer is" and 42

miaoliang
15th October 2006, 10:22
The QDataStream allows you to serialize some of the Qt data types. For QString, char pointer is right. These are my example:
First is used to write a QString into a file:

QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << "Hello World!";Second is used to read content from a file:

char *ch;
QFile file("file.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
in >> ch;
QString str(ch);The "str" is your result!