Write data from csv to float array
-
I have CSV file with 2 columns and 20 rows, how to write each column in to two float arrays. For example i could do it with QVector, but how to do it with arrays, there is array Fi and Lyamda.And how to convert QStringlist to Float?
float Fi[20]; float Lyamda[20]; int rowCount=0; QString line; QStringList qList; QFile File("F:\\data.csv"); QVector<float> z; if(File.open(QIODevice::ReadOnly)) { QTextStream out(&File); while(!out.atEnd()) { line=out.readLine(); line=line.mid(0,line.length()); qList=line.split(";"); z.append(qList[1].toFloat()); rowCount++; } File.close(); }
Regards,
Jasur -
Hi
Why do you want c type array?
Its rarely it would offer any benefits over std::vector or Qt versions.Anyway, you need to keep track free index to insert in.
// rest
int FiFree=0;while(!out.atEnd()) {
...
Fi[FiFree++] = qList[1].toFloat();- .And how to convert QStringlist to Float?
You mean each string in the list to a float value?
Just loop it over and use toFloat().
- .And how to convert QStringlist to Float?
-
-
@Jasur
but it does know qList ?
Just type toFloat() then.it does work.
This compiles and write expected output.#include <QStringlist>
#include <qDebug>float list[10];
QStringList strlist;
strlist << "1.122";
list[0]=strlist[0].toFloat();
qDebug() << list[0];update
Ahh i see what you mean.
in my sample, it wont auto complete anything on
strlist[0].
At all. Must be a bug.