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

    The Qtdata.csv always get nothing .

    Could anyone give me some advice.

    Thank you!

    Best regards,


  • Qt Champions 2017

    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.

    Again thanks for your advice.

    Best Regards,


  • Qt Champions 2017

    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.


    Best Regards,


  • Qt Champions 2017

    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.