PDA

View Full Version : How to read line from file



Krishnacins
1st June 2006, 16:31
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

jpn
1st June 2006, 16:34
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.. :)

patrik08
1st June 2006, 19:38
Work to grab line 1 or 4 to validate sqlite3 version....




QString file_get_line(QString fullFileName,int linenr)
{
QString inside ="";
QFile file(fullFileName);
int countnr = 0;
if (linenr > 0) {
if (!file.open(QFile::ReadOnly | QFile::Text)) {
return inside;
}

QTextStream in(&file);

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;
}

jacek
1st June 2006, 19:43
Work to grab line 1 or 4 to validate sqlite3 version....[...]
I don't think it will work. IMO your code is equivalent of:

...
countnr = linenr;
if( !in.atEnd() ) {
inside = in.readLine(0);
if( inside.size() > 0 ) {
return inside;
}
}
...

patrik08
1st June 2006, 20:39
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.

jacek
1st June 2006, 20:56
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.

patrik08
1st June 2006, 21:01
countnr++; so is correct?

if line request is 10 break it...

jacek
1st June 2006, 21:03
countnr++; so is correct?

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

patrik08
1st June 2006, 21:25
now run :rolleyes:





qDebug() << "### line 4 " << file_get_line("1.html",4);

QString HTML_Edit::file_get_line(QString fullFileName,int linenr)
{
QFile file(fullFileName);
QString inside = "";
if (file.exists()) {
if (file.open(QFile::ReadOnly | QFile::Text)) {
inside =file.readAll();
file.close();
}
}
QStringList list = inside.split("\n");
return QString(list.at(linenr)); /* line zero is 1 Advanced Search */
}



1.html



1 Advanced Search
2 Rate This Thread
3 ExcellentExcellent
4 GoodGood
5 AverageAverage
6 BadBad
7 TerribleTerrible
8 Posting Rules

jacek
1st June 2006, 21:56
now run :rolleyes:
Yes, but IMO your first attempt was better (except for the bug).

I would implement it like this (not tested):

QString HTML_Edit::file_get_line( const QString& fullFileName, int lineNr )
{
QString result;
QFile file( fullFileName );
if( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
QTextStream in( &file );
int currentLineNr = 0; // or 1
while( ! in.atEnd() ) {
QString line( in.readLine() );
if( currentLineNr == lineNr ) {
result = line;
break;
}
currentLineNr += 1;
}
}
return result;
}

patrik08
1st June 2006, 23:14
tested ... work .... ... is also utils to QTextStream /&/ QTcpSocket





QString HTML_Edit::file_get_line(QString fullFileName,int linenr)
{
QString result;
QFile file( fullFileName );
if( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
QTextStream in( &file );
int currentLineNr = 0; // or 1
while( ! in.atEnd() ) {
QString line( in.readLine() );
if( currentLineNr == linenr ) { /* only rewrite here .. lineNr */
result = line;
break;
}
currentLineNr += 1;
/*qDebug() << "### linerr " << currentLineNr; */
}
}
return result;
}