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. Best way to create and save QVector to CSV file
Forum Updated to NodeBB v4.3 + New Features

Best way to create and save QVector to CSV file

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 2 Posters 7.6k 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.
  • E erytcg

    I don't know why :D but data columns can't be mixed up. I want use timer to call save vectors function to file.

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

    @erytcg
    But what does it/the vectors represent ?
    You could use QPoint and one list or use a custom structure.

    Anyway, saving is pretty easy when CSV

    Something like. Its not tested and fast written so there will be syntax Terros :)

    #include <QFile>
    #include <QTextStream>
    
    void Save( QVector & v1 , QVector & v2  )
    {
        QString filename = "Data.txt";
        QFile file(filename);
        if (file.open(QIODevice::ReadWrite)) {
            QTextStream stream(&file);
           for (int c=0; c < v1.size(); c++ ) // v1 and v2 better be same size!
            stream << v1[c] << "," << v2[c]   << endl;
        }
    }
    
    1 Reply Last reply
    3
    • E Offline
      E Offline
      erytcg
      wrote on last edited by
      #5

      First column Time hh:mm:ss:zz
      2,3,4,5,6,7,8,9,10,11,12,13 column floats data i add one row per 1 ms how often I shoud save QVectors to CSV and clear memory?

      mrjjM 1 Reply Last reply
      0
      • E erytcg

        First column Time hh:mm:ss:zz
        2,3,4,5,6,7,8,9,10,11,12,13 column floats data i add one row per 1 ms how often I shoud save QVectors to CSV and clear memory?

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

        @erytcg
        How often you save really depends on the goals.
        do you want save often to prevent data loss, like pc loose power?
        or do you have memory constraints so you must get rid of data fast ?

        1 Reply Last reply
        0
        • E Offline
          E Offline
          erytcg
          wrote on last edited by
          #7

          I'm affraid of memory constraints and I don't want block data reading.

          mrjjM 1 Reply Last reply
          0
          • E erytcg

            I'm affraid of memory constraints and I don't want block data reading.

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

            @erytcg
            But how much memory does target have?

            • I don't want block data reading.
              Im not sure what you mean here.
            1 Reply Last reply
            0
            • E Offline
              E Offline
              erytcg
              wrote on last edited by
              #9

              Maybe 512MB RAM.

              mrjjM 1 Reply Last reply
              0
              • E erytcg

                Maybe 512MB RAM.

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

                @erytcg
                ok. thats not that tight.
                I assume its a light OS also ?

                I would just test it out and measure how much ram one "row" costs.
                Then you can have a pretty good idea on how often u must save.

                What kind of disk will you save to ?
                Cf cards if not industrial, can be written to death surprisingly fast even
                the later generations have become much better.

                You could also leave the file open and write each row as its complete but
                that is risky in case of power failure. (all be gone)

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  erytcg
                  wrote on last edited by erytcg
                  #11

                  Thanks for help I test time to save csv later, but i now have small problem
                  I have two methods
                  CreateFile() -> Save only first line with configuration
                  SaveData() -> save all data in columns
                  But I don't have first line only data I think that I create file two times.

                  mainwindow.cpp

                  void createFile(QString startParameters)
                  {
                       filename = QString("%1.csv").arg(QDateTime::currentDateTime().toString("ddMMyyyy-hh_mm_ss"));
                       QFile file(filename);
                       if ( file.open(QIODevice::WriteOnly) )
                       {
                            QTextStream stream( &file );
                            stream << startParameters << endl<<endl;
                       }
                       file.close();
                  }
                  
                  void saveDataToCSV(QVector<float>x1 , QVector<float> x2 , QVector<float> x3, QVector<float> x4)
                  {
                      QFile file(filename);
                      qDebug()<<filename;
                         if (file.open(QIODevice::WriteOnly)) {
                             QTextStream stream(&file);
                            for (int c=0; c < x1.size(); c++ ) 
                             stream << x1[c] << "," << x2[c]  << "," << x3[c]  << "," << x4[c]  << endl;
                         }
                         x1.clear();
                         x2.clear();
                         x3.clear();
                         x4.clear();
                         file.close();
                  }
                  

                  mainwindow.h

                  private:
                  QString startParameters;
                      QString filename;
                      QVector<float> x1;
                      QVector<float> x2;
                      QVector<float> x3;
                      QVector<float> x4;
                  
                  mrjjM 1 Reply Last reply
                  0
                  • E erytcg

                    Thanks for help I test time to save csv later, but i now have small problem
                    I have two methods
                    CreateFile() -> Save only first line with configuration
                    SaveData() -> save all data in columns
                    But I don't have first line only data I think that I create file two times.

                    mainwindow.cpp

                    void createFile(QString startParameters)
                    {
                         filename = QString("%1.csv").arg(QDateTime::currentDateTime().toString("ddMMyyyy-hh_mm_ss"));
                         QFile file(filename);
                         if ( file.open(QIODevice::WriteOnly) )
                         {
                              QTextStream stream( &file );
                              stream << startParameters << endl<<endl;
                         }
                         file.close();
                    }
                    
                    void saveDataToCSV(QVector<float>x1 , QVector<float> x2 , QVector<float> x3, QVector<float> x4)
                    {
                        QFile file(filename);
                        qDebug()<<filename;
                           if (file.open(QIODevice::WriteOnly)) {
                               QTextStream stream(&file);
                              for (int c=0; c < x1.size(); c++ ) 
                               stream << x1[c] << "," << x2[c]  << "," << x3[c]  << "," << x4[c]  << endl;
                           }
                           x1.clear();
                           x2.clear();
                           x3.clear();
                           x4.clear();
                           file.close();
                    }
                    

                    mainwindow.h

                    private:
                    QString startParameters;
                        QString filename;
                        QVector<float> x1;
                        QVector<float> x2;
                        QVector<float> x3;
                        QVector<float> x4;
                    
                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    Hi
                    If you mean to keep data in file, you must tell it so
                    file.open(QIODevice::WriteOnly)
                    ->
                    file.open(QIODevice::WriteOnly | QIODevice::Append )

                    else it will clear the file and then start writing.

                    1 Reply Last reply
                    3
                    • E Offline
                      E Offline
                      erytcg
                      wrote on last edited by
                      #13

                      You are the best! :D

                      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