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. replace a word in all line of a big txt file

replace a word in all line of a big txt file

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 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.
  • S Offline
    S Offline
    saeid0034
    wrote on 25 Oct 2020, 17:31 last edited by
    #1

    Hi i want to replace a word in all of a big txt file (txt also are in TemporaryDir)
    txt have like 20000 line and in start of all line is a word, i want to replace that word in all line
    i tested this code but its doesn't work and do nothing

    QByteArray fileData;
    QFile file(fileName);
    file.open(stderr, QIODevice::ReadWrite); // open for read and write
    fileData = file.readAll(); // read all the data into the byte array
    QString text(fileData); // add to text string for easy string replace
    
    text.replace(QString("ou"), QString("o")); // replace text in string
    
    file.seek(0); // go to the beginning of the file
    file.write(text.toUtf8()); // write the new text back to the file
    
    file.close(); // close the file handle.
    

    anybody can help with this and tell me what is best and fastest way to do this?
    thanks

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 25 Oct 2020, 17:48 last edited by
      #2

      Since you replace only ascii characters you can avoid the conversion from/to QString.
      Maybe reading/writing in chunks is needed when the file is very large.

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

      S 1 Reply Last reply 25 Oct 2020, 17:52
      1
      • C Christian Ehrlicher
        25 Oct 2020, 17:48

        Since you replace only ascii characters you can avoid the conversion from/to QString.
        Maybe reading/writing in chunks is needed when the file is very large.

        S Offline
        S Offline
        saeid0034
        wrote on 25 Oct 2020, 17:52 last edited by
        #3

        @Christian-Ehrlicher thanks for help, can you give me any example to edit in chunks?
        this code doesn't touch txt file at all, i don't know for what reason

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 25 Oct 2020, 17:54 last edited by
          #4

          @saeid0034 said in replace a word in all line of a big txt file:

          can you give me any example to edit in chunks?

          Just call QFile::read() with the chunksize, replace the text in there and write it out (to another file) and at the end replace the new with the old file.

          this code doesn't touch txt file at all, i don't know for what reason

          Check the return code of e.g. QFile::open(). You will also have to call truncate() after seek since the new file is smaller.

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

          S J 2 Replies Last reply 25 Oct 2020, 18:02
          1
          • C Christian Ehrlicher
            25 Oct 2020, 17:54

            @saeid0034 said in replace a word in all line of a big txt file:

            can you give me any example to edit in chunks?

            Just call QFile::read() with the chunksize, replace the text in there and write it out (to another file) and at the end replace the new with the old file.

            this code doesn't touch txt file at all, i don't know for what reason

            Check the return code of e.g. QFile::open(). You will also have to call truncate() after seek since the new file is smaller.

            S Offline
            S Offline
            saeid0034
            wrote on 25 Oct 2020, 18:02 last edited by
            #5

            @Christian-Ehrlicher thanks, sorry but, if you have some time can you show me a example from it

            1 Reply Last reply
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 25 Oct 2020, 18:35 last edited by
              #6

              Simply copying from someone else will not help you.

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

              S 1 Reply Last reply 25 Oct 2020, 19:12
              1
              • C Christian Ehrlicher
                25 Oct 2020, 18:35

                Simply copying from someone else will not help you.

                S Offline
                S Offline
                saeid0034
                wrote on 25 Oct 2020, 19:12 last edited by saeid0034
                #7

                @Christian-Ehrlicher so i use this code

                            QFile source(temporaryDir.path() + "/list.txt");
                            source.open(QIODevice::ReadOnly);
                            QFile destination(temporaryDir.path() + "/list2.txt");
                            destination.open(QIODevice::WriteOnly);
                            QByteArray buffer;
                            int chunksize = 2000; //chunk size
                            while(!(buffer = source.read(chunksize)).isEmpty()){
                                buffer.replace(QString("[025]"), QByteArray("test t")); //string
                                destination.write(buffer);
                            }
                            destination.close();
                            source.close();
                

                but still some line doesn't replace

                at the end i come up with this code, its working well

                            QFile data(temporaryDir.path() + "/list.txt");
                            data.open(QIODevice::Text | QIODevice::ReadOnly);
                            QString dataText = data.readAll();
                
                            dataText.replace(QString("[a]"), QByteArray("test ft")); // replace text in string
                
                            QFile newData(temporaryDir.path() + "/list2.txt");
                            if(newData.open(QFile::WriteOnly | QFile::Truncate)) {
                                QTextStream out(&newData);
                                out << dataText;
                            }
                            newData.close();
                            data.close();
                

                its Ok?

                1 Reply Last reply
                0
                • C Christian Ehrlicher
                  25 Oct 2020, 17:54

                  @saeid0034 said in replace a word in all line of a big txt file:

                  can you give me any example to edit in chunks?

                  Just call QFile::read() with the chunksize, replace the text in there and write it out (to another file) and at the end replace the new with the old file.

                  this code doesn't touch txt file at all, i don't know for what reason

                  Check the return code of e.g. QFile::open(). You will also have to call truncate() after seek since the new file is smaller.

                  J Offline
                  J Offline
                  JonB
                  wrote on 26 Oct 2020, 09:12 last edited by
                  #8

                  @Christian-Ehrlicher said in replace a word in all line of a big txt file:

                  Just call QFile::read() with the chunksize, replace the text in there

                  No, naughty Christian! You won't recognise if the text to replace if it happens to be split across a read-chunk boundary! :) Either you would have to buffer-up, or recognise end of lines, or use readLine().

                  @saeid0034
                  Regardless of strings vs bytes issues, your first solution is too risky because it uses chunk-reading, as per the issue just mentioned.

                  Your second solution is OK, provided you don't mind reading the whole of the file into memory.

                  1 Reply Last reply
                  3
                  • C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 26 Oct 2020, 17:48 last edited by
                    #9

                    @JonB said in replace a word in all line of a big txt file:

                    No, naughty Christian! You won't recognise if the text to replace if it happens to be split across a read-chunk boundary! :)

                    Correct, I was aware of this but first the basics and not a simple 'I don't know and please write code for me' :)

                    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
                    1
                    • C Online
                      C Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 26 Oct 2020, 17:51 last edited by
                      #10

                      You still convert to QString and back for no good reason. Mixing QString and QByteArray should also avoided.

                      QFile data(temporaryDir.path() + "/list.txt");
                      data.open(QIODevice::Text | QIODevice::ReadOnly);
                      
                      QByteArray dataText = data.readAll();
                      dataText.replace("[a]", "test ft");
                      
                      QFile newData(temporaryDir.path() + "/list2.txt");
                      newData.write(dataText);
                      newData.close();
                      data.close();
                      

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

                      S 1 Reply Last reply 26 Oct 2020, 20:40
                      3
                      • C Christian Ehrlicher
                        26 Oct 2020, 17:51

                        You still convert to QString and back for no good reason. Mixing QString and QByteArray should also avoided.

                        QFile data(temporaryDir.path() + "/list.txt");
                        data.open(QIODevice::Text | QIODevice::ReadOnly);
                        
                        QByteArray dataText = data.readAll();
                        dataText.replace("[a]", "test ft");
                        
                        QFile newData(temporaryDir.path() + "/list2.txt");
                        newData.write(dataText);
                        newData.close();
                        data.close();
                        
                        S Offline
                        S Offline
                        saeid0034
                        wrote on 26 Oct 2020, 20:40 last edited by saeid0034
                        #11

                        @Christian-Ehrlicher im want to replace a folder name from user pc in txt, and maybe the folder name contain some Unicode character...
                        so its better to use qstring
                        and about Mixing QString and QByteArray, it was a mistake

                        Correct, I was aware of this but first the basics and not a simple 'I don't know and please write code for me' :)

                        And one other thing, i never said to write this code for me :| , i just said do you have any example code for me, so i can learn from it...
                        anyway thanks for help

                        1 Reply Last reply
                        0

                        10/11

                        26 Oct 2020, 17:51

                        • Login

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