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
Forum Updated to NodeBB v4.3 + New Features

Write data from csv to float array

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.7k 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 2 Sept 2017, 13:50 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 2 Sept 2017, 14:57 last edited by mrjj 9 Feb 2017, 15:00
      #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 2 Sept 2017, 17:28
      0
      • D Offline
        D Offline
        dheerendra
        Qt Champions 2022
        wrote on 2 Sept 2017, 15:15 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
        • M mrjj
          2 Sept 2017, 14:57

          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 2 Sept 2017, 17:28 last edited by
          #4

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

          M 1 Reply Last reply 2 Sept 2017, 17:32
          0
          • J Jasur
            2 Sept 2017, 17:28

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 2 Sept 2017, 17:32 last edited by
            #5

            @Jasur
            In what didnt work ?

            J 1 Reply Last reply 2 Sept 2017, 17:37
            0
            • M mrjj
              2 Sept 2017, 17:32

              @Jasur
              In what didnt work ?

              J Offline
              J Offline
              Jasur
              wrote on 2 Sept 2017, 17:37 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

              M 1 Reply Last reply 2 Sept 2017, 17:40
              0
              • J Jasur
                2 Sept 2017, 17:37

                @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

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 2 Sept 2017, 17:40 last edited by mrjj 9 Feb 2017, 17:43
                #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 2 Sept 2017, 17:51
                0
                • M mrjj
                  2 Sept 2017, 17:40

                  @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 2 Sept 2017, 17:51 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 2 Sept 2017, 17:59 last edited by
                    #9

                    Great thanks to you both.

                    1 Reply Last reply
                    1

                    1/9

                    2 Sept 2017, 13:50

                    • Login

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