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. Trying to open and read a .txt file, but it deletes the actual text from the original file
Forum Updated to NodeBB v4.3 + New Features

Trying to open and read a .txt file, but it deletes the actual text from the original file

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 498 Views 3 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.
  • W Offline
    W Offline
    windygrass32
    wrote on last edited by Christian Ehrlicher
    #1

    I have a problem with my code which is the following:

    void Quiz::on_actionSelect_Questionnaire_triggered()
    {
        QString filename = QFileDialog::getOpenFileName(this,
                                                        tr("Open File"),
                                                        "/Users/katwagner/Documents/Programming 2/Assignment/Quizzes",
                                                        "All Files (*.*);;Text File (*.txt)");
        QFile file(filename);
    
        if(!file.open(QFile::WriteOnly | QFile::Text))
        {
            QMessageBox::warning(this, tr("File Not Open"),"File Cannot Be Opened");
        }
    
        QTextStream in(&file);
        QRegularExpression re("Name: ");
        QString title = in.readLine();
        title.remove(re);
        ui->label_4->setText(title);
        file.flush();
        file.close();
    }
    

    It does not show the line on the label, and it deletes all text from the original selected file. Any idea?

    M C Pablo J. RoginaP 3 Replies Last reply
    0
    • W windygrass32

      I have a problem with my code which is the following:

      void Quiz::on_actionSelect_Questionnaire_triggered()
      {
          QString filename = QFileDialog::getOpenFileName(this,
                                                          tr("Open File"),
                                                          "/Users/katwagner/Documents/Programming 2/Assignment/Quizzes",
                                                          "All Files (*.*);;Text File (*.txt)");
          QFile file(filename);
      
          if(!file.open(QFile::WriteOnly | QFile::Text))
          {
              QMessageBox::warning(this, tr("File Not Open"),"File Cannot Be Opened");
          }
      
          QTextStream in(&file);
          QRegularExpression re("Name: ");
          QString title = in.readLine();
          title.remove(re);
          ui->label_4->setText(title);
          file.flush();
          file.close();
      }
      

      It does not show the line on the label, and it deletes all text from the original selected file. Any idea?

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      @windygrass32 said in Trying to open and read a .txt file, but it deletes the actual text from the original file:

      if(!file.open(QFile::WriteOnly | QFile::Text))

      That's what you open the file for ...

      1 Reply Last reply
      4
      • W windygrass32

        I have a problem with my code which is the following:

        void Quiz::on_actionSelect_Questionnaire_triggered()
        {
            QString filename = QFileDialog::getOpenFileName(this,
                                                            tr("Open File"),
                                                            "/Users/katwagner/Documents/Programming 2/Assignment/Quizzes",
                                                            "All Files (*.*);;Text File (*.txt)");
            QFile file(filename);
        
            if(!file.open(QFile::WriteOnly | QFile::Text))
            {
                QMessageBox::warning(this, tr("File Not Open"),"File Cannot Be Opened");
            }
        
            QTextStream in(&file);
            QRegularExpression re("Name: ");
            QString title = in.readLine();
            title.remove(re);
            ui->label_4->setText(title);
            file.flush();
            file.close();
        }
        

        It does not show the line on the label, and it deletes all text from the original selected file. Any idea?

        C Offline
        C Offline
        Chrisw01
        wrote on last edited by
        #3

        @windygrass32 Open the file for reading, not writing. QFile::ReadOnly.

        1 Reply Last reply
        1
        • W windygrass32

          I have a problem with my code which is the following:

          void Quiz::on_actionSelect_Questionnaire_triggered()
          {
              QString filename = QFileDialog::getOpenFileName(this,
                                                              tr("Open File"),
                                                              "/Users/katwagner/Documents/Programming 2/Assignment/Quizzes",
                                                              "All Files (*.*);;Text File (*.txt)");
              QFile file(filename);
          
              if(!file.open(QFile::WriteOnly | QFile::Text))
              {
                  QMessageBox::warning(this, tr("File Not Open"),"File Cannot Be Opened");
              }
          
              QTextStream in(&file);
              QRegularExpression re("Name: ");
              QString title = in.readLine();
              title.remove(re);
              ui->label_4->setText(title);
              file.flush();
              file.close();
          }
          

          It does not show the line on the label, and it deletes all text from the original selected file. Any idea?

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @windygrass32 said in Trying to open and read a .txt file, but it deletes the actual text from the original file:

          In addition to what others pointed out regarding the file mode you need to use, please be aware that your ward is useless. You check for file not being open Ok, and log the situation, but you still go on and use the non-open file with the stream.

           QFile file(filename);
           
               if(!file.open(QFile::WriteOnly | QFile::Text))
               {
                   QMessageBox::warning(this, tr("File Not Open"),"File Cannot Be Opened");
               }
           
               QTextStream in(&file)
               ...
               ...
          

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          3
          • M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            In fact I'm suprised that the file is emptied because the read instruction should do nothing but setting an error.
            I really suspect the flush instruction to be the culprit.

            1 Reply Last reply
            1
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @mpergand said in Trying to open and read a .txt file, but it deletes the actual text from the original file:

              In fact I'm suprised that the file is emptied

              Doc: QIODevice::WriteOnly: The device is open for writing. Note that, for file-system subclasses (e.g. QFile), this mode implies Truncate unless combined with ReadOnly, Append or NewOnly.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              4

              • Login

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