Results 1 to 12 of 12

Thread: Read QString from file

  1. #1
    Join Date
    Jun 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default Read QString from file

    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:
    Qt Code:
    1. QString HeaderId("SME");
    2. qint32 version = 0;
    3. out << HeaderId << version;
    To copy to clipboard, switch view to plain text mode 

    I read in the reference that a QString is written as a qint32 so I tried to read it like this:
    Qt Code:
    1. int32 HeaderId[3];
    2. int32 HeaderVersion;
    3.  
    4. fread( &HeaderId, sizeof( int32), sizeof(HeaderId), file);
    5. fread( &HeaderVersion, sizeof( int32), 1, file);
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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

  4. #4
    Join Date
    Jun 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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)

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. QFile file;
    2. QTextStream out(&file);
    3. //...code for opening the file...
    4. QString HeaderId("SME");
    5. qint32 version = 0;
    6. out << HeaderId << QString::number(version);
    To copy to clipboard, switch view to plain text mode 
    reading:
    Qt Code:
    1. QFile file;
    2. QTextStream in(&file);
    3. //...code for opening the file...
    4. QString header;
    5. in>>header; //this will read "SME0", which you will have to parse.
    To copy to clipboard, switch view to plain text mode 

    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).
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Jun 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Read QString from file

    Why don't you use QSettings?
    You can save binary data by passing a QByteArray.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Read QString from file

    Quote Originally Posted by justin123 View Post
    Writing it was easy but reading it back in my Qt app was difficult.
    What exactly was difficult? Did you use QDomDocument?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jun 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?
    Last edited by wysota; 16th March 2011 at 22:23. Reason: removed URLs

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Read QString from file

    Quote Originally Posted by justin123 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 28th March 2009, 15:37
  2. is qt phonon can read realmedia file and divx file
    By fayssalqt in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2009, 15:42
  3. How can I read binary data to QString?
    By zolookas in forum Newbie
    Replies: 2
    Last Post: 29th July 2008, 20:03
  4. QDataStream and QHash<QString,QVariant> crash on read
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2008, 12:14
  5. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.