Text file's content deleted every time program runs



  • Hi, I have to add information to a text file but every time I open the program the previous contents of the file are deleted. This is the code:

    The following is what is defined in the QMainWindows builder. Where flow1 and file1 are pointers defined with the private label in QMainWindows class

     file1= new QFile ("Repairs and failures.txt");
     file1->open(QIODevice::WriteOnly);
     flow1= new QTextStream (file1);
    

    The following is the code that is performed in a clicked event

        file1->atEnd();
        QDate date;
        QString fecha;
        fecha=date.currentDate().toString("dd.MM.yyyy");
        QString texto;
        texto=ui->textEdit->toPlainText();
        if(texto.size()>1)
        {
    
            (*flow1)<<fecha<<":"<<texto<<endl;
    
        }
    

    What I'm doing wrong?

    Thanks and regards


  • Moderators

    See flags QIODevice::OpenMode. With QIODevice::WriteOnly the device will be truncated if possible. What you want is QIODevice::Append.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.