Re: Read QString from file
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.
Re: Read QString from file
See Data stream format.
Quote:
Originally Posted by Qt Documentation
QString
* If the string is null: 0xFFFFFFFF (quint32)
* Otherwise: The string length in bytes (quint32) followed by the data in UTF-16
Re: Read QString from file
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?
Re: Read QString from file
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?
Re: Read QString from file
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)
Re: Read QString from file
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:
Code:
//...code for opening the file...
qint32 version = 0;
out << HeaderId <<
QString::number(version
);
reading:
Code:
//...code for opening the file...
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).
Re: Read QString from file
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.:(
Re: Read QString from file
Why don't you use QSettings?
You can save binary data by passing a QByteArray.
Re: Read QString from file
Quote:
Originally Posted by
justin123
Writing it was easy but reading it back in my Qt app was difficult.
What exactly was difficult? Did you use QDomDocument?
Re: Read QString from file
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?
Re: Read QString from file
Quote:
Originally Posted by
justin123
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?
Quote:
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.