Solved 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
-
Hi
Do you get absolutely
nothing in the file or justblackblank lines?You should check the value of datanum
qDebug() << "lines:" << datanum; -
@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!
-
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,
Tyler_QQ
-
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(&file
name); -
-
@Tyler_QQ
Super. Please mark as solved.