Results 1 to 4 of 4

Thread: get data from readLine()

  1. #1
    Join Date
    Oct 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default get data from readLine()

    Hi guys,

    I have a question regarding get data from readLine() function.

    Currently, my function read line by line from a text file.
    Each line has two numbers with a space between them. (eg. 234.23 454.32).
    Is there a way to get those two number separately?

    Here is my code.
    Qt Code:
    1. bool getInfo(QString fileName) {
    2. QFile myFile(fileName);
    3. if(!myFile.exists()){
    4. qDebug()<<"The file"<<myFile.fileName()<<"does not exist.";
    5. return false;
    6. }
    7. if(!myFile.open(QIODevice::ReadOnly | QIODevice::Text)){
    8. qFatal("Could not open the file");
    9. return false;
    10. }
    11.  
    12. QTextStream st(&myFile);
    13.  
    14. while (!st.atEnd()){
    15. QString text;
    16. text=st.readLine();
    17. qDebug() << text;
    To copy to clipboard, switch view to plain text mode 

    In the code each line is stored in text and displayed by qDebug(). I would like to also display each numbers in a line sperately like x value is 234.23 and y value is 454.32.

    Any idea?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get data from readLine()

    Qt Code:
    1. while(!st.atEnd()) {
    2. qreal one, two;
    3. st >> one >> two;
    4. qDebug() << one << two;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: get data from readLine()

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. while(!st.atEnd()) {
    2. qreal one, two;
    3. st >> one >> two;
    4. qDebug() << one << two;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Thank you so much for your helping. Now it displays each number.
    But it displays 0 0 at the end. Do you know what is the cause?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get data from readLine()

    Most likely you have a newline character as the last character of the file hence atEnd() returns false but it is not possible to read values.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Readline from a particular index
    By giusepped in forum Qt Programming
    Replies: 5
    Last Post: 30th April 2010, 20:58
  2. QBuffer::readLine()
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 23rd April 2010, 20:57
  3. readLine in QProcess
    By grisson in forum Qt Programming
    Replies: 7
    Last Post: 15th October 2009, 17:39
  4. Qtextstream::readline()
    By dawn_to_dusk_ in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2008, 17:58
  5. readLine problem
    By gQt in forum Qt Programming
    Replies: 10
    Last Post: 29th February 2008, 09:35

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.