PDA

View Full Version : Read QString from file



justin123
15th March 2011, 03:30
How exactly do I read a QString from a file outside of Qt? The file stores map data for my game I'm writing in Visual Studio. right now I'm simply trying to read the header that I wrote to the file first.

I'm using QDataStream to write it:


QString HeaderId("SME");
qint32 version = 0;
out << HeaderId << version;


I read in the reference that a QString is written as a qint32 so I tried to read it like this:


int32 HeaderId[3];
int32 HeaderVersion;

fread( &HeaderId, sizeof( int32), sizeof(HeaderId), file);
fread( &HeaderVersion, sizeof( int32), 1, file);


I'm getting a unhandled exception when it tries to read the version. How do I do this right?

squidge
15th March 2011, 07:46
Look at the file you created in a hex editor.

Then print to the console sizeof(int32) and sizeof(HeaderId)

A better way would be class based system, where you can simply pass the class to a QDataStream and the class implements the appropriate operators to process the request, then you can use the same code to read it back again.

MarekR22
15th March 2011, 08:57
See Data stream format (http://doc.trolltech.com/latest/datastreamformat.html).

QString
* If the string is null: 0xFFFFFFFF (quint32)
* Otherwise: The string length in bytes (quint32) followed by the data in UTF-16

justin123
15th March 2011, 21:13
Still can't figure this out. I'm a noob when it comes to strings since there are so many definitions of it. So if the size of the string is written first as a int32, how do I read the utf-16? What data type should I use?

wysota
15th March 2011, 22:35
Maybe you just shouldn't be using QDataStream to write the file in the first place? Why don't you just use QFile::write() or QTextStream?

squidge
15th March 2011, 23:03
Or even go for an easily extendable file format such as XML, then when you release another version of your app with more features you don't have to write extra code to convert your old data file to your new format, as it'll be automatically compatible with all newer versions of your app (any newly introduced required elements would assume default values) and also would also be backwards compatible (older versions of your app simply ignore the extra data)

high_flyer
16th March 2011, 09:08
Amm... is it only me or is the problem really simple?
The only thing which is not clear is what is th format you want for you header?
In your code the result is (provided all the data are strings): "SME0"
Is it what you want?
Or maybe more like:
"SME\n"
"0"
At any rate you need to know what format you want.
Then you just write it with that format, and use it to parse after reading:
Writing:


QFile file;
QTextStream out(&file);
//...code for opening the file...
QString HeaderId("SME");
qint32 version = 0;
out << HeaderId << QString::number(version);

reading:


QFile file;
QTextStream in(&file);
//...code for opening the file...
QString header;
in>>header; //this will read "SME0", which you will have to parse.


But why don't you just use QSettings?
Saves all the trouble for you, you only need to write your values and give them keys (names).

justin123
16th March 2011, 16:08
I chose QDataStream because I'm storing hundreds of tile images in png format I thought it would be easier to use. I did try a XML based format. Writing it was easy but reading it back in my Qt app was difficult. I guess I'm too used to TinyXml.:(

high_flyer
16th March 2011, 16:14
Why don't you use QSettings?
You can save binary data by passing a QByteArray.

wysota
16th March 2011, 18:45
Writing it was easy but reading it back in my Qt app was difficult.
What exactly was difficult? Did you use QDomDocument?

justin123
16th March 2011, 22:14
I did use QDomDocument to write it and I tried to use QXmlStreamReader to read it back. It was getting more complex to read it than I needed it to be. But anyways it reads and writes fine in my Qt app using QDataStream. I'm just having problems with my MapReader component in my game.

I opened my saved file in a hex editor and got this:

Image removed

This is what I have in the debugger in Visual Studio:

Image removed

Does this look correct?

wysota
16th March 2011, 22:22
I did use QDomDocument to write it and I tried to use QXmlStreamReader to read it back.
So why didn't you use QDomDocument for reading as well?


But anyways it reads and writes fine in my Qt app using QDataStream. I'm just having problems with my MapReader component in my game.
The thing is QDataStream uses a custom protocol which you have to implement if you want to do the i/o without Qt. It might be easier for you to settle either on a custom binary format or use xml (you can always encode images and other binary stuff using base64 encoding or something similar.