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 to replace a particular text in a file located at any place..?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.4k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    Ketan Shah
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Ketan Shah
        wrote on last edited by
        #3

        Thanx...it worked..

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          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.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          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