Results 1 to 11 of 11

Thread: How to read line from file

  1. #1
    Join Date
    Mar 2006
    Posts
    20
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default How to read line from file

    Hi,
    i want to read file according given line number.........
    but i think there is no fuction in QFile & QTextStream class that can read file according line number

    like realine(lineNumber);
    Thanks
    Krishna

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to read line from file

    You're right. You'll have to read all lines until you reach the wanted line. It's not possible to know where does a text file contain line breaks without reading the contents..
    J-P Nurmi

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

    Default Re: How to read line from file

    Work to grab line 1 or 4 to validate sqlite3 version....

    Qt Code:
    1. QString file_get_line(QString fullFileName,int linenr)
    2. {
    3. QString inside ="";
    4. QFile file(fullFileName);
    5. int countnr = 0;
    6. if (linenr > 0) {
    7. if (!file.open(QFile::ReadOnly | QFile::Text)) {
    8. return inside;
    9. }
    10.  
    11. QTextStream in(&file);
    12.  
    13. while (!in.atEnd()) { ////// eben nicht am ende
    14. ++countnr;
    15. if (countnr == linenr) {
    16. inside = in.readLine(0);
    17. if (inside.size() > 0) {
    18. return inside;
    19. }
    20. break;
    21. }
    22. }
    23. file.close();
    24. }
    25. return inside;
    26. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to read line from file

    Quote Originally Posted by patrik08
    Work to grab line 1 or 4 to validate sqlite3 version....[...]
    I don't think it will work. IMO your code is equivalent of:
    Qt Code:
    1. ...
    2. countnr = linenr;
    3. if( !in.atEnd() ) {
    4. inside = in.readLine(0);
    5. if( inside.size() > 0 ) {
    6. return inside;
    7. }
    8. }
    9. ...
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to read line from file

    Your think that a break on line x ... read stream from 0 to x line?....

    this function i use inside to http://sourceforge.net/projects/qtexcel-xslt/ to check if file line 1
    beginn startw... "SQLite format 3" work.... mayby is possibel you have reason...

    I take only this doc....

    QString QTextStream::readLine ( qint64 maxlen = 0 )
    Reads one line of text from the stream, and returns it as a QString. The maximum allowed line length is set to maxlen. If the stream contains lines longer than this, then the lines will be split after maxlen characters and returned in parts.
    If maxlen is 0, the lines can be of any length. A common value for maxlen is 75.

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

    Default Re: How to read line from file

    Quote Originally Posted by patrik08
    Your think that a break on line x ... read stream from 0 to x line?....

    this function i use inside to http://sourceforge.net/projects/qtexcel-xslt/ to check if file line 1
    beginn startw... "SQLite format 3" work.... mayby is possibel you have reason...
    The problem is that you don't read all lines before the linenr line --- you just increment the counter.

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

    Default Re: How to read line from file

    countnr++; so is correct?

    if line request is 10 break it...

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

    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.

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

    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 

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

    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 

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

    patrik08 (1st June 2006)

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.