Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

replace a word in all line of a big txt file



  • 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


  • Lifetime Qt Champion

    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.



  • @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


  • Lifetime Qt Champion

    @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.



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


  • Lifetime Qt Champion

    Simply copying from someone else will not help you.



  • @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?



  • @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.


  • Lifetime Qt Champion

    @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' :)


  • Lifetime Qt Champion

    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();
    


  • @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


Log in to reply