Results 1 to 7 of 7

Thread: QTextStream and end of file

  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.

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

    Default Re: QTextStream and end of file

    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.


  3. #3
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextStream and end of file

    Thanks, Wysota. However, I'm a bit unclear about what you mean. I thought I used atEnd() but in my example it does not work. The for loop still messes up.

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

    Default Re: QTextStream and end of file

    You used QTextStream::atEnd() and not QFile::atEnd().

    However it might be that I misunderstood your problem. Here is an alternative:

    Qt Code:
    1. while(!file.atEnd()) {
    2. stream >> someVariable;
    3. if(stream.status() != QTextStream::Ok) break; // check status before using the data
    4. doSomethingWith(someVariable);
    5. }
    To copy to clipboard, switch view to plain text mode 
    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.


  5. The following user says thank you to wysota for this useful post:

    timmu (10th September 2012)

  6. #5
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextStream and end of file

    Thanks! I was also wondering how expensive this is

    Qt Code:
    1. if(stream.status() != QTextStream::Ok) break;
    To copy to clipboard, switch view to plain text mode 

    I need to do it very many times (hundreds of millions). How much overhead does it add?

    And this is still using QTextStream. So I still need QTextStream?

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

    Default Re: QTextStream and end of file

    You don't need QTextStream if you don't want it. Checking the status is cheap, it's just a variable in the stream object. However using the stream itself already adds some overhead.
    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.


  8. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextStream and end of file

    with stl streams, the best usage is normally

    Qt Code:
    1. while(stream >> var)
    2. {
    3. // do stuff
    4. }
    To copy to clipboard, switch view to plain text mode 

    I don't know how well that usage/paradigm works with Qt streams.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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.