Results 1 to 4 of 4

Thread: Binary QFile error

  1. #1
    Join Date
    Jan 2010
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Binary QFile error

    Hey Community,

    I am running the following piece of code on a Windows 7 machine and the output seems really strange. I get a string length of 0 back.
    Am I missing something?

    Thanks a lot
    Alex

    Qt Code:
    1. // Save the file
    2. QFile fileOne("test.file");
    3. fileOne.open(QIODevice::WriteOnly);
    4. QDataStream out(&fileOne);
    5. out.setVersion(QDataStream::Qt_4_6);
    6. out << "Test";
    7. fileOne.close();
    8.  
    9. //load from it
    10. QFile file("test.file");
    11. file.open(QIODevice::ReadOnly);
    12. QDataStream in(&file);
    13. QString str;
    14. in.setVersion(QDataStream::Qt_4_6);
    15. in >> str ;
    16.  
    17. QString strLength;
    18. strLength.setNum(str.length());
    19.  
    20. QMessageBox::information(0,"Output",str);
    21. QMessageBox::information(0,"Output",strLength);
    22. file.close();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Binary QFile error

    from the documentation for QDataStream:
    Each item written to the stream is written in a predefined binary format that varies depending on the item's type. Supported Qt types include QBrush, QColor, QDateTime, QFont, QPixmap, QString, QVariant and many others. For the complete list of all Qt types supporting data streaming see the Format of the QDataStream operators.
    you are reading it out as a QString, so you must also write it in as a QString

    you need to change:
    out << "test"
    to
    out << QString("test")

  3. #3
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Binary QFile error

    Try:
    Qt Code:
    1. out << QString("Test");
    To copy to clipboard, switch view to plain text mode 

    /edit:
    I was too slow...

  4. #4
    Join Date
    Jan 2010
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Binary QFile error

    Argh.. could've seen that one myself.

    Thanks you two...

Similar Threads

  1. Replies: 0
    Last Post: 4th November 2009, 10:21
  2. QFile, QDataStream reading binary data
    By yren in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2009, 06:34
  3. OS/X Qt/Mac 3.3.x binary installers
    By hvengel in forum Installation and Deployment
    Replies: 0
    Last Post: 17th September 2007, 00:17
  4. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58
  5. xml with binary question
    By TheKedge in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2006, 23:21

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.