Results 1 to 8 of 8

Thread: How to read line number in a text file

  1. #1
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default How to read line number in a text file

    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.
    Thanks & Regards
    Sandeep G.R.

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read line number in a text file

    hey in your code itself u can check the line number ...

    Qt Code:
    1. if(linenumber == 3)
    2. printf( "%3d: %s\n", linenumber, line.latin1() );
    3.  
    4. linenumber++;
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to read line number in a text file

    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.
    Thanks & Regards
    Sandeep G.R.

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read line number in a text file

    QString linenumber;
    int lineNo;
    bool ok;
    linenumber = lineEdit->text()
    lineNo = linenumber.toInt(&ok, 10);
    then the condition
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to read line number in a text file

    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.
    Last edited by grsandeep85; 30th July 2009 at 13:19.
    Thanks & Regards
    Sandeep G.R.

  6. #6
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read line number in a text file

    please use [CODE] tag

    Qt Code:
    1. void DatumForm::readfile()
    2. {
    3. QString resulttemp;
    4. int count = 0;
    5. bool ok;
    6. resulttemp = datumnolineEdit->text();
    7. int lineNo = resulttemp.toInt(&ok, 10);
    8.  
    9. QFile file( "datum.txt" );
    10. if ( file.open( IO_ReadWrite ) ) {
    11. QTextStream stream( &file );
    12.  
    13. while ( !stream.atEnd() ) {
    14. count += 1;
    15. line = stream.readLine( );
    16. if(lineNo == count)
    17. printf( "%3d: %s\n", count, line.latin1() );
    18.  
    19. }
    20. file.close();
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wagmare; 30th July 2009 at 13:27. Reason: change in int lineNo
    "Behind every great fortune lies a crime" - Balzac

  7. #7
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to read line number in a text file

    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.
    Last edited by grsandeep85; 31st July 2009 at 08:28.
    Thanks & Regards
    Sandeep G.R.

  8. #8
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to read line number in a text file

    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();
    }
    Thanks & Regards
    Sandeep G.R.

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. Continuous read of text from an input file
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 00:09
  3. Replies: 5
    Last Post: 27th May 2009, 12:49
  4. Text file to PDF convertion using C++ code
    By joseph in forum General Discussion
    Replies: 2
    Last Post: 21st August 2008, 01:28
  5. Replies: 13
    Last Post: 1st June 2006, 14:01

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.