I am trying to write a program that will create a text file and write a stream of characters that I read from a serial port. I am just trying to understand how I can get the code to continuously write the data and not overwrite the previously written data because I will need it to keep every character sent. I wrote this code as a test but I can’t get it to continuously write the text. It writes it once and then stops.
#include <QFile>
#include <QTextStream>
int main() {
int test = 0;
while(test < 2){
QString filename = "Data.txt";
QFile file(filename);
if (file.open(QIODevice::ReadWrite)) {
QTextStream stream(&file);
stream << "one thing to test" << endl;
test++;
}
}
}
Anyone have any suggestions?
-Thanks
By default, opening with
QIODevice::ReadWritewill truncate the file on write. According to the documentation,QIODevice::Appendwill open the file for writing at the end of the file.If you really want to both read and write, you could
QIODevice::seek()to the end of the file before writing.