Problem with reading double values form txt file
-
I have .txt file that looks something like this:
1.3472
0.4883
1
....
3.4883I need to store this values in array, vector or something else but I can't find the way how to do it?
If someone can help me out here it would help me a lot.
Thanks -
I have .txt file that looks something like this:
1.3472
0.4883
1
....
3.4883I need to store this values in array, vector or something else but I can't find the way how to do it?
If someone can help me out here it would help me a lot.
ThanksHi, and welcome!
@Programelllo said in Problem with reading double values form txt file:
I can't find the way how to do it?
What programming experience do you have? Who gave you this task?
If this is for a course or class, what do your notes suggest?
-
Hi, and welcome!
@Programelllo said in Problem with reading double values form txt file:
I can't find the way how to do it?
What programming experience do you have? Who gave you this task?
If this is for a course or class, what do your notes suggest?
@JKSH I'm working on ui that converts money currencies
And because of it coefficient variability i stored coefficient in xlsx file that i converted in txt file so i need now to read that coefficients. It's project for faculty, and i haven't got any help -
@JKSH I'm working on ui that converts money currencies
And because of it coefficient variability i stored coefficient in xlsx file that i converted in txt file so i need now to read that coefficients. It's project for faculty, and i haven't got any help@Programelllo
I don't know what "coefficient" is about. But all you have to do is pick whichever C++ library call you wish to use to read the lines from file and convert todouble
numbers (e.g. https://doc.qt.io/qt-5/qtextstream.html#readLine + https://doc.qt.io/qt-5/qstring.html#toDouble, or https://doc.qt.io/qt-5/qtextstream.html#operator-gt-gt-11), and then store them in, say,QVector<double>
. Which bit is a problem? -
@JKSH I'm working on ui that converts money currencies
And because of it coefficient variability i stored coefficient in xlsx file that i converted in txt file so i need now to read that coefficients. It's project for faculty, and i haven't got any helphi
@Programelllo said in Problem with reading double values form txt file:And because of it coefficient variability i stored coefficient in xlsx file
Because they vary very often you can get them directly from a webservice.
For reading a text file line by line you can use QFile class
QFile txtFile(fileName); if (txtFile.open(QIODevice::ReadOnly)) { QTextStream in(&txtFile); while (!in.atEnd()) { QString line = in.readLine(); ... } txtFile.close(); }
-
@JKSH I'm working on ui that converts money currencies
And because of it coefficient variability i stored coefficient in xlsx file that i converted in txt file so i need now to read that coefficients. It's project for faculty, and i haven't got any help@Programelllo said in Problem with reading double values form txt file:
stored coefficient in xlsx file that i converted in txt file
Qt can read xlsx directly using the ODBC SQL Driver: https://wiki.qt.io/Handling_Microsoft_Excel_file_format#Using_ODBC
Small tweak to @LeLev program:
QVector<double> values; QFile txtFile(fileName); if (txtFile.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&txtFile); QString line; QLocale locale; // this determines decimal and thousand separators double value=0.0; bool ok=false; while (stream.readLineInto(&line)) { value=locale.toDouble(line,&ok); if(ok) values << value; } }
-
hi
@Programelllo said in Problem with reading double values form txt file:And because of it coefficient variability i stored coefficient in xlsx file
Because they vary very often you can get them directly from a webservice.
For reading a text file line by line you can use QFile class
QFile txtFile(fileName); if (txtFile.open(QIODevice::ReadOnly)) { QTextStream in(&txtFile); while (!in.atEnd()) { QString line = in.readLine(); ... } txtFile.close(); }
@LeLev i just need to know how this line convert to double?
-
@LeLev i just need to know how this line convert to double?
@Programelllo Well, you can read the documentation: https://doc.qt.io/qt-5/qstring.html#toDouble
Or read what @VRonin wrote. -
@Programelllo Well, you can read the documentation: https://doc.qt.io/qt-5/qstring.html#toDouble
Or read what @VRonin wrote.@jsulm how than i access to certain member of vector values?
-
@Programelllo said in Problem with reading double values form txt file:
stored coefficient in xlsx file that i converted in txt file
Qt can read xlsx directly using the ODBC SQL Driver: https://wiki.qt.io/Handling_Microsoft_Excel_file_format#Using_ODBC
Small tweak to @LeLev program:
QVector<double> values; QFile txtFile(fileName); if (txtFile.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&txtFile); QString line; QLocale locale; // this determines decimal and thousand separators double value=0.0; bool ok=false; while (stream.readLineInto(&line)) { value=locale.toDouble(line,&ok); if(ok) values << value; } }
@VRonin your code solved my problem thank you very much