Results 1 to 11 of 11

Thread: How to read line from file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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
    countnr++; so is correct?

    if line request is 10 break it...
    Yes, but you read only the first line from the file.

  2. #2
    Join Date
    May 2006
    Posts
    788
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    49
    Thanked 48 Times in 46 Posts

    Default Re: How to read line from file

    now run


    Qt Code:
    1. qDebug() << "### line 4 " << file_get_line("1.html",4);
    2.  
    3. QString HTML_Edit::file_get_line(QString fullFileName,int linenr)
    4. {
    5. QFile file(fullFileName);
    6. QString inside = "";
    7. if (file.exists()) {
    8. if (file.open(QFile::ReadOnly | QFile::Text)) {
    9. inside =file.readAll();
    10. file.close();
    11. }
    12. }
    13. QStringList list = inside.split("\n");
    14. return QString(list.at(linenr)); /* line zero is 1 Advanced Search */
    15. }
    To copy to clipboard, switch view to plain text mode 



    1.html

    Qt Code:
    1. 1 Advanced Search
    2. 2 Rate This Thread
    3. 3 ExcellentExcellent
    4. 4 GoodGood
    5. 5 AverageAverage
    6. 6 BadBad
    7. 7 TerribleTerrible
    8. 8 Posting Rules
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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 

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

    patrik08 (1st June 2006)

  5. #4
    Join Date
    May 2006
    Posts
    788
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    49
    Thanked 48 Times in 46 Posts

    Default Re: How to read line from file

    tested ... work .... ... is also utils to QTextStream /&/ QTcpSocket


    Qt Code:
    1. QString HTML_Edit::file_get_line(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 ) { /* only rewrite here .. lineNr */
    11. result = line;
    12. break;
    13. }
    14. currentLineNr += 1;
    15. /*qDebug() << "### linerr " << currentLineNr; */
    16. }
    17. }
    18. return result;
    19. }
    To copy to clipboard, switch view to plain text mode 

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.