Results 1 to 7 of 7

Thread: QTextStream and end of file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextStream and end of file

    What is the best way to stop QTextStream input at end of file.
    This reads past end of file because apaprently the end of row character is considered an entry. What clause to use with QTextStream to detect for that? Normally I would use !file.eof() but this does not work here. My input file consists of several rows of numbers (all rows contain the same number of values).

    My code

    Qt Code:
    1. QFile file(Name); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {exit(1);}
    2. QTextStream my_stream(&file);
    3.  
    4. while(!my_stream.atEnd())
    5. {
    6.  
    7. for (k=0; k<100; k++)
    8. {
    9. double readInput;
    10. my_stream >> readInput;
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    This would work but it's not elegant:
    (and it actually has a problem since one wrong value is read)

    Qt Code:
    1. bool OK=1;
    2. while(!my_stream.atEnd() && OK)
    3. {
    4.  
    5. for (k=0; k<100; k++)
    6. {
    7. if(OK)
    8. {
    9. double readInput;
    10. my_stream >> readInput;
    11. }
    12. if(my_stream.atEnd()) OK=0;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    I need to read my file one entry at a time but readLine() wouldn't work since I don't want to convert QString to double every time.
    Last edited by timmu; 10th September 2012 at 13:27.

Similar Threads

  1. Creating a new File using QFile and QTextStream
    By jshafferman in forum Qt Programming
    Replies: 1
    Last Post: 7th September 2011, 16:20
  2. QTextStream and file handle
    By Dilshad in forum Newbie
    Replies: 2
    Last Post: 4th January 2011, 11:26
  3. Replies: 0
    Last Post: 17th June 2010, 11:07
  4. writing file with QTextStream
    By Raul in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2010, 14:46
  5. Reading File using QFile and QTextStream
    By atm in forum Qt Programming
    Replies: 4
    Last Post: 13th July 2006, 23:12

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.