Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Text file's content deleted every time program runs
Qt 6.11 is out! See what's new in the release blog

Text file's content deleted every time program runs

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 544 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JulianJ Offline
    JulianJ Offline
    Julian
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

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

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved