How to write buffer's value into .csv file?



  • Hi all,

    I have the buffer and wanted to write the buffer's value in the .csv file.

    Here is my code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWidget>
    #include <QProcess>
    #include <QThread>
    #include <QList>
    #include <QStringList>
    #include <QDir>
    #include <QDebug>
    #include <QString>
    #include <QFileDialog>
    #include <QMessageBox>
    #include <QFile>
    #include <stdio.h>
    #include "unistd.h"
    
    .......
    
    QString filename = "/home/tyler/test2/Qtdata.csv";
    QFile file( filename );
    if( file.open(QFile::ReadWrite)){
       QTextStream out(&filename);
       for(int i = 0; i < datanum; i++){
             QString tmp = QString("%1,%2,%3\n").arg(b_data[0][i]).arg(b_data[1][i]).arg(b_data[2][i]);
             out << tmp;
               }              
      }
      file.close();
    

    The Qtdata.csv always get nothing .

    Could anyone give me some advice.

    Thank you!

    Best regards,

    Tyler_QQ


  • Qt Champions 2016

    Hi
    Do you get absolutely
    nothing in the file or just black blank lines?

    You should check the value of datanum
    qDebug() << "lines:" << datanum;


  • Moderators

    @Tyler_QQ said in How to write buffer's value in to .csv file?:

    QTextStream out(&filename);

    should be

    QTextStream out(&file);
    

    You're writing into the string containing the file name!



  • @mrjj

    Hi mrjj,

    Thanks for your reply.

    There is just blank line in my .csv file.

    The value of datanum and tmp's three value are correct.
    0_1479801703448_2016-11-22_160103.jpg

    Again thanks for your advice.

    Best Regards,

    Tyler_QQ


  • Qt Champions 2016

    And @jsulm sharp eye caught the real bug.
    You are giving it the filename and not file :)
    file and not filename should be used as he mentions.
    So you write nothing to the file but all to your string :)

    QTextStream out(&filename);



  • @jsulm

    Hi jsulm,

    Thanks a lot!!

    Through your advice, I had fixed this error.

    0_1479802113294_2016-11-22_160818.jpg

    Best Regards,

    Tyler_QQ


  • Qt Champions 2016

    @Tyler_QQ
    Super. Please mark as solved.


Log in to reply
 

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