Results 1 to 5 of 5

Thread: QDataStream reading into QString

  1. #1
    Join Date
    Jan 2006
    Posts
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    Qt Code:
    1. QFile outFile( "output.txt" );
    2. outFile.open( IO_WriteOnly );
    3.  
    4. QDataStream outStream( &outFile );
    5.  
    6. outStream << "The beginning of a series of numbers";
    7.  
    8. for (int i = 0; i < 50000; i++)
    9. {
    10.  
    11. outStream << (Q_INT32) i;
    12.  
    13. }/* end loop through numbers */
    14.  
    15. double outFloat = 3.1516342315;
    16.  
    17. double* addressOfFloat = &outFloat;
    18.  
    19. outStream << outFloat;
    20.  
    21. outStream << "The end of the series of numbers. Have a nice day.";
    22.  
    23. outFile.close( );
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. QFile inFile( "output.txt" );
    2. inFile.open( IO_ReadOnly );
    3. QDataStream inStream( &inFile );
    4.  
    5. Q_INT32 data[50000];
    6.  
    7. QString string;
    8.  
    9. inStream >> string;
    10.  
    11. for (int i = 0; i < 50000; i++)
    12. {
    13.  
    14. inStream >> data[i];
    15.  
    16. }
    17.  
    18. double doubleCheck;
    19. inStream >> doubleCheck;
    20.  
    21. inStream >> string;
    22.  
    23. inFile.close();
    To copy to clipboard, switch view to plain text mode 

    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.
    The end of time is reached around the year 8000, by which time we expect Qt to be obsolete.

    -Note in QDate documentation. It's nice that Qt has been able to predict when the world will end.

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream reading into QString

    what strings actually insteads?
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream reading into QString

    You did not write a QString but instead a
    Qt Code:
    1. const char *
    To copy to clipboard, switch view to plain text mode 
    This
    Qt Code:
    1. outStream << QString("The beginning of a series of numbers");
    To copy to clipboard, switch view to plain text mode 
    should work. Or you could change the type you are trying to read out of the stream.

  4. #4
    Join Date
    Jan 2006
    Posts
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream reading into QString

    Quote Originally Posted by Codepoet
    You did not write a QString but instead a
    Qt Code:
    1. const char *
    To copy to clipboard, switch view to plain text mode 
    This
    Qt Code:
    1. outStream << QString("The beginning of a series of numbers");
    To copy to clipboard, switch view to plain text mode 
    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
    The end of time is reached around the year 8000, by which time we expect Qt to be obsolete.

    -Note in QDate documentation. It's nice that Qt has been able to predict when the world will end.

  5. #5
    Join Date
    Oct 2006
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking 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:
    Qt Code:
    1. QFile file("file.dat");
    2. file.open(QIODevice::WriteOnly);
    3. QDataStream out(&file);
    4. out << "Hello World!";
    To copy to clipboard, switch view to plain text mode 
    Second is used to read content from a file:
    Qt Code:
    1. char *ch;
    2. QFile file("file.dat");
    3. file.open(QIODevice::ReadOnly);
    4. QDataStream in(&file);
    5. in >> ch;
    6. QString str(ch);
    To copy to clipboard, switch view to plain text mode 
    The "str" is your result!

Similar Threads

  1. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 21:53
  2. Replies: 8
    Last Post: 27th August 2007, 16:45
  3. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59
  4. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 21:23
  5. QDataStream >> QString
    By smalls in forum Qt Programming
    Replies: 2
    Last Post: 17th January 2006, 23:14

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.