Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Write data from csv to float array

Write data from csv to float array

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jasur
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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().
      J 1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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();
        }

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        2
        • mrjjM mrjj

          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().
          J Offline
          J Offline
          Jasur
          wrote on last edited by
          #4

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

          mrjjM 1 Reply Last reply
          0
          • J Jasur

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Jasur
            In what didnt work ?

            J 1 Reply Last reply
            0
            • mrjjM mrjj

              @Jasur
              In what didnt work ?

              J Offline
              J Offline
              Jasur
              wrote on last edited by
              #6

              @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

              mrjjM 1 Reply Last reply
              0
              • J Jasur

                @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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

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

                J 1 Reply Last reply
                0
                • mrjjM mrjj

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

                  J Offline
                  J Offline
                  Jasur
                  wrote on last edited by
                  #8

                  @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

                  1 Reply Last reply
                  1
                  • J Offline
                    J Offline
                    Jasur
                    wrote on last edited by
                    #9

                    Great thanks to you both.

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved