Best way to create and save QVector to CSV file



  • Hello how I can easy create new .csv files and save QVectors to next columns.
    For example

    QVector v1 = (1,2,3,4,5);
    QVector v2 = (6,7,8,9,10);
    

    csv file:

    SOME text in first row
    1,6
    2,7
    3,8
    4,9
    5,10
    

  • Qt Champions 2016

    Hi
    Why do you need it to be stored in different vectors if they belong together ?



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


  • Qt Champions 2016

    @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;
        }
    }
    


  • 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?


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    @erytcg
    But how much memory does target have?

    • I don't want block data reading.
      Im not sure what you mean here.


  • Maybe 512MB RAM.


  • Qt Champions 2016

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



  • 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;
    

  • Qt Champions 2016

    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.



  • You are the best! :D


Log in to reply
 

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