QDataStream reading into QString
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.
Code:
QFile outFile
( "output.txt" );
outFile.open( IO_WriteOnly );
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.
Code:
QFile inFile
( "output.txt" );
inFile.open( IO_ReadOnly );
Q_INT32 data[50000];
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.
Re: QDataStream reading into QString
what strings actually insteads?
Re: QDataStream reading into QString
You did not write a QString but instead a This
Code:
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.
Re: QDataStream reading into QString
Quote:
Originally Posted by Codepoet
You did not write a QString but instead a
This
Code:
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
Quote:
Originally Posted by 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
Re: QDataStream reading into QString
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:
Code:
out << "Hello World!";
Second is used to read content from a file:
Code:
char *ch;
in >> ch;
The "str" is your result!