Qasim Qasim - 1 month ago 8
C++ Question

Write a new text without losing the previous value.in Qt

How can i Write a new text without losing the previous value

QString mFilename2 = "bin/bin_2.txt";
File_main_Editor.stWrite(mFilename2,okline_Edit);





void stWrite(QString Filename,QString stringtext){
QFile mFile(Filename);

if(!mFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox message_file_Write;
message_file_Write.warning(0,"Open Error"
,"could not to open file for Writing");
return;
}
QTextStream out(&mFile);
out << stringtext;
out.setCodec("UTF-8");

mFile.flush();
mFile.close();
}


Every time that okline_Edit is initialized
stWrite function called a new value in the file is poured .txt previous value is lost.

Or in other words
enter image description here

enter image description here

Answer

You need to set QIODevice::Append when you open() the file.

mFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)

Additionally, if you want each append to be on a new line, you will have to insert a \n as well.