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. Using CStdioFile to modify text file

Using CStdioFile to modify text file

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
2 Posts 2 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.
  • S Offline
    S Offline
    shultas
    wrote on last edited by
    #1

    Hey all. I've got a text file that I am trying to modify, but am running into some problems. Here's the code that I am using: CStdioFile myFile; myFile.Open("c:\\temp\\test1.txt",CFile::modeReadWrite); myFile.Seek(40,CFile::begin); myFile.WriteString("this is the replaced line\n"); myFile.Close(); It kind of works, but what is happening is it is overwriting the contents of the next lines and I can't figure out how to get around this. Ie, the file looks like this before I run it line 0 line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 then, after i execute the above statements line 0 line 1 line 2 line 3 line 4 this is the replaced line e 8 line 9 So, when the newly modified text is written, if it happens to be longer than the original line, it will write over other lines below it, instead of "appending" so to speak, making the file look like: ... line 3 line 4 this is the replaced line line 6 line 7 line 8 ... if I do something like "...WriteString("line Z"), it replaces it fine because the new string matches the length of the one that's in the file. Any ideas on how to replace an entire line in a text file (each line is ended with \n) without overwriting following lines? Thanks

    M 1 Reply Last reply
    0
    • S shultas

      Hey all. I've got a text file that I am trying to modify, but am running into some problems. Here's the code that I am using: CStdioFile myFile; myFile.Open("c:\\temp\\test1.txt",CFile::modeReadWrite); myFile.Seek(40,CFile::begin); myFile.WriteString("this is the replaced line\n"); myFile.Close(); It kind of works, but what is happening is it is overwriting the contents of the next lines and I can't figure out how to get around this. Ie, the file looks like this before I run it line 0 line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 then, after i execute the above statements line 0 line 1 line 2 line 3 line 4 this is the replaced line e 8 line 9 So, when the newly modified text is written, if it happens to be longer than the original line, it will write over other lines below it, instead of "appending" so to speak, making the file look like: ... line 3 line 4 this is the replaced line line 6 line 7 line 8 ... if I do something like "...WriteString("line Z"), it replaces it fine because the new string matches the length of the one that's in the file. Any ideas on how to replace an entire line in a text file (each line is ended with \n) without overwriting following lines? Thanks

      M Offline
      M Offline
      Member 162462
      wrote on last edited by
      #2

      U have to store remaining lines( after 4 line) in some other temporary buffer , then insert what ever content u want insert , once again append temporary buffer.

      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