Results 1 to 11 of 11

Thread: How to read line from file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: How to read line from file

    Quote Originally Posted by patrik08
    now run
    Yes, but IMO your first attempt was better (except for the bug).

    I would implement it like this (not tested):
    Qt Code:
    1. QString HTML_Edit::file_get_line( const QString& fullFileName, int lineNr )
    2. {
    3. QString result;
    4. QFile file( fullFileName );
    5. if( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
    6. QTextStream in( &file );
    7. int currentLineNr = 0; // or 1
    8. while( ! in.atEnd() ) {
    9. QString line( in.readLine() );
    10. if( currentLineNr == lineNr ) {
    11. result = line;
    12. break;
    13. }
    14. currentLineNr += 1;
    15. }
    16. }
    17. return result;
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to jacek for this useful post:

    patrik08 (1st June 2006)

Similar Threads

  1. read file from end to beginning..
    By soul_rebel in forum Qt Programming
    Replies: 11
    Last Post: 4th June 2012, 01:20
  2. Replies: 13
    Last Post: 1st June 2006, 14:01
  3. How to read encoding type in XML file
    By danbr in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2006, 08:17
  4. QListWidget-problem
    By Sarma in forum Qt Programming
    Replies: 7
    Last Post: 7th April 2006, 18:49
  5. How to detect new line?
    By whoops.slo in forum Newbie
    Replies: 6
    Last Post: 6th January 2006, 17:02

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.