Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to Replace a line in a .TXT file

How to Replace a line in a .TXT file

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
4 Posts 4 Posters 0 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.
  • Z Offline
    Z Offline
    zxc89
    wrote on last edited by
    #1

    Hi.. How can we replace a line in a .TXT file using C++?? My aim is to replace a particular line in a ABC.TXT with another line from a ZYWV.TXT file?? Plz HELP ME!! Thanks.

    I D S 3 Replies Last reply
    0
    • Z zxc89

      Hi.. How can we replace a line in a .TXT file using C++?? My aim is to replace a particular line in a ABC.TXT with another line from a ZYWV.TXT file?? Plz HELP ME!! Thanks.

      I Offline
      I Offline
      Ivy_Venkat
      wrote on last edited by
      #2

      User fscanf/fread to scan through the file and go to the required line Once the required line is found, use fseek to go back to the start of the line Do a fprintf/fwrite again. Keep in mind that if the new line is longer that the line in the file, it will overwrite entries in the next line. You will have to devise a appropriate insertion/modify strategy if the line lengths can be different.

      1 Reply Last reply
      0
      • Z zxc89

        Hi.. How can we replace a line in a .TXT file using C++?? My aim is to replace a particular line in a ABC.TXT with another line from a ZYWV.TXT file?? Plz HELP ME!! Thanks.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        From ABC.TXT, write all lines up to the line to be replaced into a temporary file. Write the new line into the temporary file. Write the remaining lines from ABC.TXT into the temporary file. Delete ABC.TXT. Rename the temporary file to ABC.TXT. Make sense?


        "The largest fire starts but with the smallest spark." - David Crow

        "Judge not by the eye but by the heart." - Native American Proverb

        1 Reply Last reply
        0
        • Z zxc89

          Hi.. How can we replace a line in a .TXT file using C++?? My aim is to replace a particular line in a ABC.TXT with another line from a ZYWV.TXT file?? Plz HELP ME!! Thanks.

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          Try something like this (haven't tested this). ---------------------------------------------- #incluce #include #include int main(int argc, char* argv[]) { using namespace std; // Open files. ifstream inf("C:\\in.txt"); if (!inf) { cerr << "Failed to open input file" << endl; return 1; } ofstream outf("C:\\out.txt"); if (!outf) { cerr << "Failed to open output file" << endl; return 1; } // Copy the input file to the output file line by line replacing as needed. string line; while (getline(inf, line)) { if (line=="Replace this line") { outf << "With this line" << endl; } else { outf << line << endl; } } return 0; } Steve

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups