PDA

View Full Version : How to read line number in a text file



grsandeep85
30th July 2009, 10:31
Hi All,

have a text file in the format
#n X Y Z
0 123 456 789
1 321 654 987
2 345 243 809
......
.....
...... and so on
i have implemented the code which reads the contents of file and display in the console along with the line numbers, here is my code

QStringList lines;
QString result;
QFile file( "datum.txt" );
if ( file.open( IO_ReadWrite ) ) {
QTextStream stream( &file );
QString line;
linenumber = 0;
while ( !stream.atEnd() ) {
line = stream.readLine( );
printf( "%3d: %s\n", linenumber++, line.latin1() );
lines +=line;
}
file.close();
}

now how read the particular line number in a text file(say line number 3) and its contents.please help us it would be a great pleasure.

wagmare
30th July 2009, 10:47
hey in your code itself u can check the line number ...


if(linenumber == 3)
printf( "%3d: %s\n", linenumber, line.latin1() );

linenumber++;

grsandeep85
30th July 2009, 10:56
That line number input will be given from user in the QLineEdit then that line number contents should be read and put into a QString. how to do this.

wagmare
30th July 2009, 11:03
QString linenumber;
int lineNo;
bool ok;
linenumber = lineEdit->text()
lineNo = linenumber.toInt(&ok, 10);
then the condition

grsandeep85
30th July 2009, 12:27
I implemented your logic but i am getting different values means its reading the last line of the text file and here is my code

void DatumForm::readfile()
{
datumnolineEdit->setFocus();
resulttemp = datumnolineEdit->text();
decimals_mm_datum = 3;
QStringList lines;
QString resulttemp;
QFile file( "datum.txt" );
if ( file.open( IO_ReadWrite ) ) {
QTextStream stream( &file );
QString line;
while ( !stream.atEnd() ) {
line = stream.readLine( );
linenumber = resulttemp.toInt(&ok, 10);
if ( linenumber == resulttemp )
{

printf( "%3d: %s\n", linenumber, line.latin1() );
linenumber++;
lines +=line;
}
resulttemp = line;

qDebug("Read a particular Datum");
printf( "%3d: %s\n", linenumber, resulttemp.latin1() );
datumlines = QStringList::split(",", resulttemp);

qDebug ( "Datum number : %d",Datumno_Val);
Xdatum_Val = datumlines[0].toDouble();
xdatcountlineEdit->setText(QString::number( Xdatum_Val, 'f', decimals_mm_datum) );
qDebug ( "Xdatum_Val : %4.3lf",Xdatum_Val);

Ydatum_Val = datumlines[1].toDouble();
ydatcountlineEdit->setText(QString::number( Ydatum_Val, 'f', decimals_mm_datum) );
qDebug ( "Ydatum_Val : %4.3lf",Ydatum_Val);

Zdatum_Val = datumlines[2].toDouble();
zdatcountlineEdit->setText(QString::number( Zdatum_Val, 'f', decimals_mm_datum) );
qDebug ( "Zdatum_Val : %4.3lf",Zdatum_Val);
}
file.close();
}
}

how to proceed further.

wagmare
30th July 2009, 13:19
please use
tag

[CODE]void DatumForm::readfile()
{
QString resulttemp;
int count = 0;
bool ok;
resulttemp = datumnolineEdit->text();
int lineNo = resulttemp.toInt(&ok, 10);

QFile file( "datum.txt" );
if ( file.open( IO_ReadWrite ) ) {
QTextStream stream( &file );

while ( !stream.atEnd() ) {
count += 1;
line = stream.readLine( );
if(lineNo == count)
printf( "%3d: %s\n", count, line.latin1() );

}
file.close();
}

grsandeep85
31st July 2009, 08:00
Thanks its working fine but during the startup its displaying the last linenumber content how to overcome this issue. the contents are printed in the console terminal and also i need to split the read line and display in the line edits.

grsandeep85
31st July 2009, 09:09
I solved the problem and my code is here...

QString resulttemp;
QString line;
int count = -1;
bool ok;
resulttemp = datumnolineEdit->text();
int linenumber = resulttemp.toInt(&ok, 10);

QFile file( "datum.txt" );
if ( file.open( IO_ReadWrite ) ) {
QTextStream stream( &file );

while ( !stream.atEnd() ) {
count += 1;
line = stream.readLine( );

if(linenumber == count){
resulttemp = line;
datumlines = QStringList::split(",", resulttemp);

qDebug ( "Datum number : %d",Datumno_Val);
Xdatum_Val = datumlines[0].toDouble();
xdatcountlineEdit->setText(QString::number( Xdatum_Val, 'f', decimals_mm_datum) );
qDebug ( "Xdatum_Val : %4.3lf",Xdatum_Val);

Ydatum_Val = datumlines[1].toDouble();
ydatcountlineEdit->setText(QString::number( Ydatum_Val, 'f', decimals_mm_datum) );
qDebug ( "Ydatum_Val : %4.3lf",Ydatum_Val);

Zdatum_Val = datumlines[2].toDouble();
zdatcountlineEdit->setText(QString::number( Zdatum_Val, 'f', decimals_mm_datum) );
qDebug ( "Zdatum_Val : %4.3lf",Zdatum_Val);

qDebug ("Result values of a corresponding datum points");
printf( "%3d: %s\n", count, line.latin1() );
}
}


file.close();
}