0

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

MD XF
  • 7,860
  • 7
  • 40
  • 71
Qasim
  • 83
  • 10
  • try this `mFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)` [ref. for OpenModeFlag](http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum) – saygins Nov 06 '16 at 20:41

2 Answers2

0

You open file with QIODevice::WriteOnly that would start writing...well from start, you need to open it with QIODevice::Append

Oleg Bogdanov
  • 1,712
  • 13
  • 19
0

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.

dtech
  • 47,916
  • 17
  • 112
  • 190