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
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
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
Work to grab line 1 or 4 to validate sqlite3 version....
Qt Code:
{ int countnr = 0; if (linenr > 0) { return inside; } while (!in.atEnd()) { ////// eben nicht am ende ++countnr; if (countnr == linenr) { inside = in.readLine(0); if (inside.size() > 0) { return inside; } break; } } file.close(); } return inside; }To copy to clipboard, switch view to plain text mode
I don't think it will work. IMO your code is equivalent of:Originally Posted by patrik08
Qt Code:
... countnr = linenr; if( !in.atEnd() ) { inside = in.readLine(0); if( inside.size() > 0 ) { return inside; } } ...To copy to clipboard, switch view to plain text mode
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.
The problem is that you don't read all lines before the linenr line --- you just increment the counter.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.Originally Posted by patrik08
now run
Qt Code:
qDebug() << "### line 4 " << file_get_line("1.html",4); { if (file.exists()) { inside =file.readAll(); file.close(); } } }To copy to clipboard, switch view to plain text mode
1.html
Qt Code:
1 Advanced Search 2 Rate This Thread 3 ExcellentExcellent 4 GoodGood 5 AverageAverage 6 BadBad 7 TerribleTerrible 8 Posting RulesTo copy to clipboard, switch view to plain text mode
Yes, but IMO your first attempt was better (except for the bug).Originally Posted by patrik08
I would implement it like this (not tested):
Qt Code:
{ QString result; int currentLineNr = 0; // or 1 while( ! in.atEnd() ) { if( currentLineNr == lineNr ) { result = line; break; } currentLineNr += 1; } } return result; }To copy to clipboard, switch view to plain text mode
patrik08 (1st June 2006)
tested ... work .... ... is also utils to QTextStream /&/ QTcpSocket
Qt Code:
{ QString result; int currentLineNr = 0; // or 1 while( ! in.atEnd() ) { if( currentLineNr == linenr ) { /* only rewrite here .. lineNr */ result = line; break; } currentLineNr += 1; /*qDebug() << "### linerr " << currentLineNr; */ } } return result; }To copy to clipboard, switch view to plain text mode
Bookmarks