Results 1 to 7 of 7

Thread: how to read coordinates points from text file

  1. #1
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default how to read coordinates points from text file

    Hello,

    i want to read points coordinats from a text file, how can i do that please?

    2.0000000e+000 8.1000000e+001
    3.0000000e+000 6.9000000e+001
    6.0000000e+000 1.5500000e+002
    1.2000000e+001 1.2400000e+002
    1.2000000e+001 2.2800000e+002
    2.4000000e+001 1.9200000e+002
    2.4000000e+001 2.1600000e+002
    2.8000000e+001 2.3700000e+002

    there are spaces between the X and Y coordinates
    help me please and thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to read coordinates points from text file


  3. #3
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Re: how to read coordinates points from text file

    Thank you for your reply, but i tried this and i have a runTime error
    Qt Code:
    1. #include <QApplication>
    2. #include <QtWidgets>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QFile monFichier("C:/Users/amine/Downloads/dcis/dcis1.txt");
    9.  
    10. double x = 0.0;
    11. double y = 0.0;
    12.  
    13. QTextStream ts(&monFichier);
    14. QString line;
    15. line = ts.readLine();
    16. QStringList s = line.split(" ",QString::SkipEmptyParts);
    17. x = s.at(0).toDouble();
    18. y = s.at(1).toDouble();
    19.  
    20. QLCDNumber *lcd1 = new QLCDNumber(&f);
    21. lcd1->setSegmentStyle(QLCDNumber::Flat);
    22. lcd1->move(100, 100);
    23. QLCDNumber *lcd2 = new QLCDNumber(&f);
    24. lcd2->setSegmentStyle(QLCDNumber::Flat);
    25. lcd2->move(170, 100);
    26.  
    27. lcd1->display(x);
    28. lcd2->display(y);
    29. f.show();
    30.  
    31.  
    32. return app.exec();
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 


    Added after 10 minutes:


    there are some spaces in the begining and between the coordinates
    Last edited by rafik; 25th January 2016 at 09:05.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to read coordinates points from text file

    Read about QString::trimmed() - manual is the best friend the programmer.

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to read coordinates points from text file

    If I remember correctly, QTextStream's extraction operator (>>) interprets whitespace as a separator between values. Perhaps all you have to do is:
    Qt Code:
    1. double x, y;
    2. ts >> x >> y;
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to yeye_olive for this useful post:

    rafik (25th January 2016)

  7. #6
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Re: how to read coordinates points from text file

    thank you all for your replies , yeye_olive your instruction give the result, thank you


    Added after 31 minutes:


    i have another problem, i don't want to read the last line in the text file, because if i do a loop and read the coordinates the program display x as 0.0 and y as 0.0
    so my question is : to stop reading coordinates when it reaches the line before the last line if it is possible.
    How can i do that please.
    Qt Code:
    1. #include <QApplication>
    2. #include <QtWidgets>
    3.  
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QFile monFichier("C:/Users/amine/Downloads/dcis/dcis1.txt");
    10. monFichier.open(QIODevice::ReadOnly);
    11.  
    12. double x = 0.0;
    13. double y = 0.0;
    14.  
    15. QTextStream ts(&monFichier);
    16. while(!ts.atEnd())
    17. {
    18. ts >> x >> y;
    19. }
    20.  
    21. QLCDNumber *lcd1 = new QLCDNumber(&f);
    22. lcd1->setSegmentStyle(QLCDNumber::Flat);
    23. lcd1->move(100, 100);
    24. QLCDNumber *lcd2 = new QLCDNumber(&f);
    25. lcd2->setSegmentStyle(QLCDNumber::Flat);
    26. lcd2->move(170, 100);
    27.  
    28. lcd1->display(x);
    29. lcd2->display(y);
    30. f.show();
    31.  
    32. return app.exec();
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rafik; 25th January 2016 at 11:11.

  8. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to read coordinates points from text file

    Read data with first method (readLine()) and test whether the length is greater then 0.

  9. The following user says thank you to Lesiok for this useful post:

    rafik (4th February 2016)

Similar Threads

  1. How to read the text of a pdf file?
    By Momergil in forum Newbie
    Replies: 4
    Last Post: 22nd January 2016, 10:45
  2. Qt 5 Read a Text File With Delimiters
    By te777 in forum Qt Programming
    Replies: 4
    Last Post: 9th November 2014, 14:25
  3. Read from text file
    By kulsekarr in forum Newbie
    Replies: 3
    Last Post: 8th June 2012, 12:11
  4. Replies: 2
    Last Post: 12th September 2008, 08:43
  5. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 08:47

Tags for this Thread

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.