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. Copy and paste contents of one file to another and modify
QtWS25 Last Chance

Copy and paste contents of one file to another and modify

Scheduled Pinned Locked Moved Solved General and Desktop
file
8 Posts 3 Posters 1.7k 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.
  • R Offline
    R Offline
    russjohn834
    wrote on 18 May 2021, 09:28 last edited by
    #1

    Hi All,

    I'm a bit stuck in a problem. I need to do the following file operation

    1. Need to make a new file and copy content from an old file + modify it by adding a few more extra bits.
    2. Delete the old file.

    Here I could open the file in readwrite mode and read line by line.

    void MainWindow::on_pushButton_readlog_clicked()
    {
        QString filename = "oldfile.txt";
        QFile originalFile(filename);
    
        if(!originalFile.open(QIODevice::ReadWrite ))
        {
            qDebug () << "Error opening Log file: "<<originalFile.errorString();
            return;
    
        }
        else
        {
            QTextStream instream(& originalFile);
            QString line = instream.readLine();
            while(!instream.atEnd())
            {
                QString line =instream.readLine(); // I can read line by line
                qDebug()<<line;
            }
            originalFile.close();
    
        }
    
    }
    

    How do I copy this contents into a new file and add few more lines of texts to it?
    Say line 1: "Name: new name"
    line 2: "Device ID: new ID"

    J J 2 Replies Last reply 18 May 2021, 09:32
    0
    • R russjohn834
      18 May 2021, 09:28

      Hi All,

      I'm a bit stuck in a problem. I need to do the following file operation

      1. Need to make a new file and copy content from an old file + modify it by adding a few more extra bits.
      2. Delete the old file.

      Here I could open the file in readwrite mode and read line by line.

      void MainWindow::on_pushButton_readlog_clicked()
      {
          QString filename = "oldfile.txt";
          QFile originalFile(filename);
      
          if(!originalFile.open(QIODevice::ReadWrite ))
          {
              qDebug () << "Error opening Log file: "<<originalFile.errorString();
              return;
      
          }
          else
          {
              QTextStream instream(& originalFile);
              QString line = instream.readLine();
              while(!instream.atEnd())
              {
                  QString line =instream.readLine(); // I can read line by line
                  qDebug()<<line;
              }
              originalFile.close();
      
          }
      
      }
      

      How do I copy this contents into a new file and add few more lines of texts to it?
      Say line 1: "Name: new name"
      line 2: "Device ID: new ID"

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 18 May 2021, 09:32 last edited by
      #2

      @russjohn834 said in Copy and paste contents of one file to another and modify:

      How do I copy this contents into a new file

      https://doc.qt.io/qt-5/qfile.html#copy-1

      "and add few more lines of texts to it?" - by opening the file for writing and appending the lines. What exactly is the problem?

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

      1 Reply Last reply
      2
      • R russjohn834
        18 May 2021, 09:28

        Hi All,

        I'm a bit stuck in a problem. I need to do the following file operation

        1. Need to make a new file and copy content from an old file + modify it by adding a few more extra bits.
        2. Delete the old file.

        Here I could open the file in readwrite mode and read line by line.

        void MainWindow::on_pushButton_readlog_clicked()
        {
            QString filename = "oldfile.txt";
            QFile originalFile(filename);
        
            if(!originalFile.open(QIODevice::ReadWrite ))
            {
                qDebug () << "Error opening Log file: "<<originalFile.errorString();
                return;
        
            }
            else
            {
                QTextStream instream(& originalFile);
                QString line = instream.readLine();
                while(!instream.atEnd())
                {
                    QString line =instream.readLine(); // I can read line by line
                    qDebug()<<line;
                }
                originalFile.close();
        
            }
        
        }
        

        How do I copy this contents into a new file and add few more lines of texts to it?
        Say line 1: "Name: new name"
        line 2: "Device ID: new ID"

        J Online
        J Online
        JonB
        wrote on 18 May 2021, 09:34 last edited by
        #3

        @russjohn834
        You cannot do this by opening the existing file for read-write. As you correctly say you must copy from old to new:

        1. Open existing file for read.
        2. Open new file for write.
        3. Read line by line from old file, writing to new file. Make any amendments (e.g. changing a line or inserting a new line) as you go.
        4. Close the new file.
        5. Close the old file.
        6. Delete the old file.
        7. Now optionally rename the new file to something like the old file name if that is desired.
        R 1 Reply Last reply 18 May 2021, 10:51
        1
        • J JonB
          18 May 2021, 09:34

          @russjohn834
          You cannot do this by opening the existing file for read-write. As you correctly say you must copy from old to new:

          1. Open existing file for read.
          2. Open new file for write.
          3. Read line by line from old file, writing to new file. Make any amendments (e.g. changing a line or inserting a new line) as you go.
          4. Close the new file.
          5. Close the old file.
          6. Delete the old file.
          7. Now optionally rename the new file to something like the old file name if that is desired.
          R Offline
          R Offline
          russjohn834
          wrote on 18 May 2021, 10:51 last edited by
          #4

          @JonB @jsulm Thanks!

          @JonB said in Copy and paste contents of one file to another and modify:

          1. Read line by line from old file, writing to new file. Make any amendments (e.g. changing a line or inserting a new line) as you go.

          That's where I'm stuck!

          Contents from the existing file looks like this:

          === USAGE_LOG ===
          Power cycles: 1
          Unpause events: 1
          Power-on time: 29860s
          Unpaused time: 8s
          0	1	8074	8074	8074
          1	0	4294967295	0	0
          2	0	4294967295	0	0
          3	0	4294967295	0	0
          4	0	4294967295	0	0
          5	0	4294967295	0	0
          6	0	4294967295	0	0
          7	0	4294967295	0	0
          8	0	4294967295	0	0
          9	0	4294967295	0	0
          10	0	4294967295	0	0
          11	0	4294967295	0	0
          12	0	4294967295	0	0
          13	0	4294967295	0	0
          14	0	4294967295	0	0
          15	0	4294967295	0	0
          16	0	4294967295	0	0
          17	0	4294967295	0	0
          

          Basically I need to insert two new lines (before "=== USAGE_LOG==="). Looks like I need to get read line by line and store into a string list. Not I'm not sure how to "iterate" over the string list and modify(or insert) lines. Can you show me a way to do this?

          J 1 Reply Last reply 18 May 2021, 11:01
          0
          • R russjohn834
            18 May 2021, 10:51

            @JonB @jsulm Thanks!

            @JonB said in Copy and paste contents of one file to another and modify:

            1. Read line by line from old file, writing to new file. Make any amendments (e.g. changing a line or inserting a new line) as you go.

            That's where I'm stuck!

            Contents from the existing file looks like this:

            === USAGE_LOG ===
            Power cycles: 1
            Unpause events: 1
            Power-on time: 29860s
            Unpaused time: 8s
            0	1	8074	8074	8074
            1	0	4294967295	0	0
            2	0	4294967295	0	0
            3	0	4294967295	0	0
            4	0	4294967295	0	0
            5	0	4294967295	0	0
            6	0	4294967295	0	0
            7	0	4294967295	0	0
            8	0	4294967295	0	0
            9	0	4294967295	0	0
            10	0	4294967295	0	0
            11	0	4294967295	0	0
            12	0	4294967295	0	0
            13	0	4294967295	0	0
            14	0	4294967295	0	0
            15	0	4294967295	0	0
            16	0	4294967295	0	0
            17	0	4294967295	0	0
            

            Basically I need to insert two new lines (before "=== USAGE_LOG==="). Looks like I need to get read line by line and store into a string list. Not I'm not sure how to "iterate" over the string list and modify(or insert) lines. Can you show me a way to do this?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 18 May 2021, 11:01 last edited by jsulm
            #5

            @russjohn834 Simply open a temp file, write the new lines, then read line by line the old file and write these lines into temp file. Then replace the old file with the temp file (https://doc.qt.io/qt-5/qfile.html#remove-1, https://doc.qt.io/qt-5/qfile.html#rename-1).

            To add an entry at the beginning of a list use https://doc.qt.io/qt-5/qlist.html#prepend, iterating over a list is as easy as

            for (QString str : strList) {
            
            }
            

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

            R 1 Reply Last reply 18 May 2021, 13:58
            0
            • J jsulm
              18 May 2021, 11:01

              @russjohn834 Simply open a temp file, write the new lines, then read line by line the old file and write these lines into temp file. Then replace the old file with the temp file (https://doc.qt.io/qt-5/qfile.html#remove-1, https://doc.qt.io/qt-5/qfile.html#rename-1).

              To add an entry at the beginning of a list use https://doc.qt.io/qt-5/qlist.html#prepend, iterating over a list is as easy as

              for (QString str : strList) {
              
              }
              
              R Offline
              R Offline
              russjohn834
              wrote on 18 May 2021, 13:58 last edited by
              #6

              @jsulm @JonB

              I did the following : I stuck at two places:

              1. How to copy contents line by line from old file to new file
                (with my implementation below, it's simply copying everything from old file as single line in new file)

              2. If I simply do outstream<<line; I'm not sure how to modify it's contents.

              Do you think StringList is better in this case or is there a better way to do this?

                  QString filename = "oldfile.txt";
                  QString newfilename = "newfile.txt";
              
                  QFile originalFile(filename), newFile(newfilename);
              
                  originalFile.open(QIODevice:: ReadOnly);
                  newFile.open(QIODevice:: WriteOnly);
              
                  QTextStream instream(& originalFile);
                  QTextStream outstream(& newFile);
                  while(!instream.atEnd())
                  {
                      QString line = instream.readLine();
                      outstream<<line; // all the contents are copied as a single line here
                  }
              
                    originalFile.close();
                    newFile.close();
              
              
              
              J 1 Reply Last reply 18 May 2021, 17:04
              0
              • R russjohn834
                18 May 2021, 13:58

                @jsulm @JonB

                I did the following : I stuck at two places:

                1. How to copy contents line by line from old file to new file
                  (with my implementation below, it's simply copying everything from old file as single line in new file)

                2. If I simply do outstream<<line; I'm not sure how to modify it's contents.

                Do you think StringList is better in this case or is there a better way to do this?

                    QString filename = "oldfile.txt";
                    QString newfilename = "newfile.txt";
                
                    QFile originalFile(filename), newFile(newfilename);
                
                    originalFile.open(QIODevice:: ReadOnly);
                    newFile.open(QIODevice:: WriteOnly);
                
                    QTextStream instream(& originalFile);
                    QTextStream outstream(& newFile);
                    while(!instream.atEnd())
                    {
                        QString line = instream.readLine();
                        outstream<<line; // all the contents are copied as a single line here
                    }
                
                      originalFile.close();
                      newFile.close();
                
                
                
                J Online
                J Online
                JonB
                wrote on 18 May 2021, 17:04 last edited by JonB
                #7

                @russjohn834
                Your code looks good. instream.readLine(); should return each line separately. Then all would be well. If you cannot correctly read lines you would have the same problem if you use QStringList. Check the contents of your input file (preferably in a binary editor) for what the exact line terminator is.

                If you are on Windows (or possibly MacOS too, and it does no harm for Linux anyway) you should pass QIODevice::Text when opening the file for read. I think your failure to do so might lead to the behaviour you see (whole file as one line). That flag should also be on the file you are writing to.

                R 1 Reply Last reply 19 May 2021, 10:21
                3
                • J JonB
                  18 May 2021, 17:04

                  @russjohn834
                  Your code looks good. instream.readLine(); should return each line separately. Then all would be well. If you cannot correctly read lines you would have the same problem if you use QStringList. Check the contents of your input file (preferably in a binary editor) for what the exact line terminator is.

                  If you are on Windows (or possibly MacOS too, and it does no harm for Linux anyway) you should pass QIODevice::Text when opening the file for read. I think your failure to do so might lead to the behaviour you see (whole file as one line). That flag should also be on the file you are writing to.

                  R Offline
                  R Offline
                  russjohn834
                  wrote on 19 May 2021, 10:21 last edited by
                  #8

                  @JonB Thanks a lot. Solved it by this:
                  outstream<<line<<'\n'; :)

                  1 Reply Last reply
                  0

                  8/8

                  19 May 2021, 10:21

                  • Login

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