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. How can I delete first line of text file before write
Forum Updated to NodeBB v4.3 + New Features

How can I delete first line of text file before write

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 1.5k Views
  • 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.
  • T Offline
    T Offline
    TheCeylann
    wrote on last edited by
    #1

    I want to delete first line fo text file before write and after deleting write in first line how can I do that there is my code;

    void SecondWindow::on_pushButton_8_clicked()
    {
    
        QFile file("C:/Users/umutc/Desktop/Test/text/test.txt");
    //I need delete first line here before write 
        if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
            return;
        {
            QTextStream  out(&file);
            out << ui->newPassBox->text();
    
        }
    
        }
    
    jsulmJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Use QTextStream::readLine() and don't write out the first line.

      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
      3
      • T TheCeylann

        I want to delete first line fo text file before write and after deleting write in first line how can I do that there is my code;

        void SecondWindow::on_pushButton_8_clicked()
        {
        
            QFile file("C:/Users/umutc/Desktop/Test/text/test.txt");
        //I need delete first line here before write 
            if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
                return;
            {
                QTextStream  out(&file);
                out << ui->newPassBox->text();
        
            }
        
            }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @TheCeylann You will need to write new content into a temporary file, then replace the original file with that temporary file.

        void SecondWindow::on_pushButton_8_clicked()
        {
            QFile file("C:/Users/umutc/Desktop/Test/text/test.txt");
            QFile tmpFile("C:/Users/umutc/Desktop/Test/text/test_tmp.txt");
        
            if (!file.open(QIODevice::Read | QIODevice::Text) || !tmpFile.open(QIODevice::Write | QIODevice::Text))
                return;
            
            QTextStream  tmp(&tmpFile);
            tmp << ui->newPassBox->text();
        
            QTextStream  in(&tmpFile);
            // Skip first line
            in.readLine();
            while (!in.atEnd()) {
                tmp << in.readLine();
            }
        
            // Now replace the original file with tmp
            file.close();
            tmpFile.close();
            QFile::remove("C:/Users/umutc/Desktop/Test/text/test.txt");
            QFile::rename("C:/Users/umutc/Desktop/Test/text/test_tmp.txt", "C:/Users/umutc/Desktop/Test/text/test.txt");
        }
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        Q 1 Reply Last reply
        3
        • jsulmJ jsulm

          @TheCeylann You will need to write new content into a temporary file, then replace the original file with that temporary file.

          void SecondWindow::on_pushButton_8_clicked()
          {
              QFile file("C:/Users/umutc/Desktop/Test/text/test.txt");
              QFile tmpFile("C:/Users/umutc/Desktop/Test/text/test_tmp.txt");
          
              if (!file.open(QIODevice::Read | QIODevice::Text) || !tmpFile.open(QIODevice::Write | QIODevice::Text))
                  return;
              
              QTextStream  tmp(&tmpFile);
              tmp << ui->newPassBox->text();
          
              QTextStream  in(&tmpFile);
              // Skip first line
              in.readLine();
              while (!in.atEnd()) {
                  tmp << in.readLine();
              }
          
              // Now replace the original file with tmp
              file.close();
              tmpFile.close();
              QFile::remove("C:/Users/umutc/Desktop/Test/text/test.txt");
              QFile::rename("C:/Users/umutc/Desktop/Test/text/test_tmp.txt", "C:/Users/umutc/Desktop/Test/text/test.txt");
          }
          
          Q Offline
          Q Offline
          Qt embedded developer
          wrote on last edited by Qt embedded developer
          #4

          @jsulm said in How can I delete first line of text file before write:

          QFile::rename("C:/Users/umutc/Desktop/Test/text/test_tmp.txt", "C:/Users/umutc/Desktop/Test/text/test.txt");

          hi i have used your code for replace the first line for text file. but it delete the whole content of file and replace the text of temp file.

          why its not working ?

          QString str1 = QDir::homePath() + "/Runtime/"+ "ReadTotalPowerOnTime1.txt";
          
          
              QFile file(str1);
          
              if (!file.open(QIODevice::ReadWrite))
              {
                  qDebug() << "File Creation Failed";
                  qDebug()<< "Cannot open file for writing: "
                          << qPrintable(file.errorString());
              }
          
               QFile tmpFile("test_tmp.txt");
          
                  if(((!tmpFile.open(QIODevice::WriteOnly | QIODevice::Text))))
                     return;
          
                  QTextStream  tmp(&tmpFile);
                  tmp <<"hi";
          
                  QTextStream  in1(&tmpFile);
                  // Skip first line
                  in1.readLine();
                  while (!in1.atEnd()) {
                      tmp << in1.readLine();
                  }
          
                  // Now replace the original file with tmp
                  file.close();
                  tmpFile.close();
                  QFile::remove(str1);
                  QFile::rename("test_tmp.txt", str1);
          
          
          JonBJ 1 Reply Last reply
          0
          • Q Qt embedded developer

            @jsulm said in How can I delete first line of text file before write:

            QFile::rename("C:/Users/umutc/Desktop/Test/text/test_tmp.txt", "C:/Users/umutc/Desktop/Test/text/test.txt");

            hi i have used your code for replace the first line for text file. but it delete the whole content of file and replace the text of temp file.

            why its not working ?

            QString str1 = QDir::homePath() + "/Runtime/"+ "ReadTotalPowerOnTime1.txt";
            
            
                QFile file(str1);
            
                if (!file.open(QIODevice::ReadWrite))
                {
                    qDebug() << "File Creation Failed";
                    qDebug()<< "Cannot open file for writing: "
                            << qPrintable(file.errorString());
                }
            
                 QFile tmpFile("test_tmp.txt");
            
                    if(((!tmpFile.open(QIODevice::WriteOnly | QIODevice::Text))))
                       return;
            
                    QTextStream  tmp(&tmpFile);
                    tmp <<"hi";
            
                    QTextStream  in1(&tmpFile);
                    // Skip first line
                    in1.readLine();
                    while (!in1.atEnd()) {
                        tmp << in1.readLine();
                    }
            
                    // Now replace the original file with tmp
                    file.close();
                    tmpFile.close();
                    QFile::remove(str1);
                    QFile::rename("test_tmp.txt", str1);
            
            
            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @Qt-embedded-developer
            I see you are opening the file QIODevice::WriteOnly.

            • You then attempt to read from this file. How is that going to work?
            • Even if it did work, what is the point of reading from a file which you just opened for write and truncated to 0 length, you know what is going to be there anyway?

            Please make your code sensible before asking for help, thanks. And take the time to read and understand what @jsulm has shown you, your code is not working because it is not the same as his and does not make sense.

            UPDATE Actually, @jsulm's code is wrong. You should be able to spot where and correct, and/or he may see this and correct his post.

            Q 1 Reply Last reply
            0
            • JonBJ JonB

              @Qt-embedded-developer
              I see you are opening the file QIODevice::WriteOnly.

              • You then attempt to read from this file. How is that going to work?
              • Even if it did work, what is the point of reading from a file which you just opened for write and truncated to 0 length, you know what is going to be there anyway?

              Please make your code sensible before asking for help, thanks. And take the time to read and understand what @jsulm has shown you, your code is not working because it is not the same as his and does not make sense.

              UPDATE Actually, @jsulm's code is wrong. You should be able to spot where and correct, and/or he may see this and correct his post.

              Q Offline
              Q Offline
              Qt embedded developer
              wrote on last edited by Qt embedded developer
              #6

              @JonB can you tell me what option i need to choose ? because there are no option like QIODevice::Write .

              can anybody help to correct the code written by @jsulm

              JonBJ 1 Reply Last reply
              0
              • Q Qt embedded developer

                @JonB can you tell me what option i need to choose ? because there are no option like QIODevice::Write .

                can anybody help to correct the code written by @jsulm

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @Qt-embedded-developer

                • Your QIODevice::WriteOnly is correct, never mind that @jsulm wrote QIODevice::Write. This also applies for QIODevice::Read -> QIODevice::ReadOnly.

                • You open file.open(QIODevice::ReadWrite) for read-write. Why? You also removed the QIODevice::Text. Why?

                • The issue in @jsulm's code, replicated in yours, is that you & he have:

                QTextStream  tmp(&tmpFile);
                QTextStream  in(&tmpFile);
                

                If you take the time to think about it you will realize it cannot be right and will correct it.

                jsulmJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @Qt-embedded-developer

                  • Your QIODevice::WriteOnly is correct, never mind that @jsulm wrote QIODevice::Write. This also applies for QIODevice::Read -> QIODevice::ReadOnly.

                  • You open file.open(QIODevice::ReadWrite) for read-write. Why? You also removed the QIODevice::Text. Why?

                  • The issue in @jsulm's code, replicated in yours, is that you & he have:

                  QTextStream  tmp(&tmpFile);
                  QTextStream  in(&tmpFile);
                  

                  If you take the time to think about it you will realize it cannot be right and will correct it.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @JonB said in How can I delete first line of text file before write:

                  QTextStream tmp(&tmpFile);
                  QTextStream in(&tmpFile);

                  Yeah, that one I did not see.
                  But @Qt-embedded-developer should be able to fix that...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Q 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @JonB said in How can I delete first line of text file before write:

                    QTextStream tmp(&tmpFile);
                    QTextStream in(&tmpFile);

                    Yeah, that one I did not see.
                    But @Qt-embedded-developer should be able to fix that...

                    Q Offline
                    Q Offline
                    Qt embedded developer
                    wrote on last edited by
                    #9

                    @jsulm yes i have done it.

                    1 Reply Last reply
                    0

                    • Login

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