I have data coming in from an Arduino, which I want to write to a CSV file. The problem is the data keeps overwriting the existing line. Here's a sample of the code:
QFile data("F:/logdata.csv");
if (data.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&data);
out << hum << "," << temp << "," << gas << '\n';
}
Are you trying to reopen and append data to the same file? In that case you don't want to use the QFile::Truncate
flag since it will remove the content in that file.
Please see http://doc.qt.io/qt-5/qiodevice.html :
QIODevice::Truncate If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
Also as suggested by Ibarros use the QIODevice::Append
flag to make sure data is appended.
Update
Actually you also don't want to use the QIODevice::WriteOnly
flag too, since it will add the truncate flag. You should use QIODevice::ReadWrite
instead.
data.open(QIODevice::ReadWrite| QIODevice::Append)
When opening the file, use "QIODevice::Append" flag, so the data you write to it will be appended at the end of the file.
Example:
data.open(QFile::WriteOnly | QFile::Truncate |QIODevice::Append)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.