How to replace a particular text in a file located at any place..?



  • This is my code....I want to replace a text in a file, is this correct can any1 help me...
    @
    QFile file("E:/qtpro/Abc1.txt");
    file.open(QIODevice::ReadWrite | QIODevice::Text);
    file.reset();

    QTextStream fl(&file);
    // QTextStream f2(&file);
    QString str;

    str = fl.readLine();

    while (!str.isNull()) {
    if (str == "Shah") {
    //str.replace("Shah",";Shah",Qt::CaseSensitive);
    fl<<";Shah"<<str<<"\n";
    }
    str = fl.readLine();
    @

    [Mark up code, Tobias]
    Edit: And again, by André.


  • Moderators

    You do not want to write into the file you are reading.



  • Thanx...it worked..



  • Read the file completely into memory, change the content and then write again.
    Or create a temporary second file, read the original, change the text and write to the second one.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.