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


  • Qt Champions 2016

    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().


  • int i=0;
    while(!file.atEnd()) {
    QString line = file.readline();
    QStringList cols = line.split(",");
    fi[i] = cols.at(0).toFloat();
    lamada[i++] = cols.at(1).toFloat();
    }



  • @mrjj i tried this doesn't work Fi[FiFree++] = qList[1].toFloat();


  • Qt Champions 2016

    @Jasur
    In what didnt work ?



  • @mrjj after qList[1] i can't put dot, there is no functions for qList[1].
    P.S sorry for my english if i can't explain


  • Qt Champions 2016

    @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.



  • @mrjj yes there is no auto complete afer qList[0], oh sorry i forget to tell you, that mr.Dhreerendas code work, this is what i wanted. Thank you



  • Great thanks to you both.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.