Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[Solved] Appending to the end of a text file
Endless last edited by
In the following code snippet, there's a QString called parameterString that contains a few lines of information, and I'm trying to append it to the end of a text file.
I was using QFile::Append before, but it kept adding a phantom carriage return/line feed in between the previous end of file and the new data.
if(parameterFile.open(QFile::WriteOnly | QFile::Text))
out << parameterString;
This gives me weird results. Some of the data from the original file is there but not all. What am I doing wrong?
ChrisW67 last edited by
Opening the file WriteOnly will discard its existing content. If you seek to position n and write you will get n zero bytes in the file, followed by whatever you write.
You want to append so you should open the file for appending.
As for the "phantom CRLF". If the existing data was written with QTextStream then you should not have magically added CR LF characters. Many text editors will add CR LF to an incomplete last line.