View Full Version : how to read coordinates points from text file
rafik
25th January 2016, 08:28
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.
anda_skoa
25th January 2016, 08:32
QFile, QTextStream::readLine(), QString::split().
Cheers,
_
rafik
25th January 2016, 09:05
Thank you for your reply, but i tried this and i have a runTime error
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget f;
QFile monFichier("C:/Users/amine/Downloads/dcis/dcis1.txt");
double x = 0.0;
double y = 0.0;
QTextStream ts(&monFichier);
QString line;
line = ts.readLine();
QStringList s = line.split(" ",QString::SkipEmptyParts);
x = s.at(0).toDouble();
y = s.at(1).toDouble();
QLCDNumber *lcd1 = new QLCDNumber(&f);
lcd1->setSegmentStyle(QLCDNumber::Flat);
lcd1->move(100, 100);
QLCDNumber *lcd2 = new QLCDNumber(&f);
lcd2->setSegmentStyle(QLCDNumber::Flat);
lcd2->move(170, 100);
lcd1->display(x);
lcd2->display(y);
f.show();
return app.exec();
}
Added after 10 minutes:
there are some spaces in the begining and between the coordinates
Lesiok
25th January 2016, 09:31
Read about QString::trimmed() - manual is the best friend the programmer.
yeye_olive
25th January 2016, 09:31
If I remember correctly, QTextStream's extraction operator (>>) interprets whitespace as a separator between values. Perhaps all you have to do is:
double x, y;
ts >> x >> y;
rafik
25th January 2016, 11:11
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.
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget f;
QFile monFichier("C:/Users/amine/Downloads/dcis/dcis1.txt");
monFichier.open(QIODevice::ReadOnly);
double x = 0.0;
double y = 0.0;
QTextStream ts(&monFichier);
while(!ts.atEnd())
{
ts >> x >> y;
}
QLCDNumber *lcd1 = new QLCDNumber(&f);
lcd1->setSegmentStyle(QLCDNumber::Flat);
lcd1->move(100, 100);
QLCDNumber *lcd2 = new QLCDNumber(&f);
lcd2->setSegmentStyle(QLCDNumber::Flat);
lcd2->move(170, 100);
lcd1->display(x);
lcd2->display(y);
f.show();
return app.exec();
}
Lesiok
25th January 2016, 11:40
Read data with first method (readLine()) and test whether the length is greater then 0.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.