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 last line of a file?

How to replace last line of a file?

Scheduled Pinned Locked Moved C / C++ / MFC
c++debuggingxmltutorialquestion
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.
  • J Offline
    J Offline
    Jon Taylor
    wrote on last edited by
    #1

    I'm working on an app that dumps loads of trace information to a file, as XML: <log> <message> .... </message> </log> Now, when the app closes I write "</log>" as the last line. However, the next time the app is run, I want to append to this file, which means I must remove that last line. (It's the XML "root node" :) ) Now, I know I can rename the file, re-parse it and re-write it etc but these log files reach 10's of Mb in size, so it takes too long to do that. So I want to over-write the last line with some new text instead. I seem to remember doing this using the C standard library in the dim and distant past, but can I do this using the C++ library?!! :mad: I've tried with the <fstream> library, and seekp() & tellp() but am getting a bit unstuck :~ Any ideas anyone?

    N S 2 Replies Last reply
    0
    • J Jon Taylor

      I'm working on an app that dumps loads of trace information to a file, as XML: <log> <message> .... </message> </log> Now, when the app closes I write "</log>" as the last line. However, the next time the app is run, I want to append to this file, which means I must remove that last line. (It's the XML "root node" :) ) Now, I know I can rename the file, re-parse it and re-write it etc but these log files reach 10's of Mb in size, so it takes too long to do that. So I want to over-write the last line with some new text instead. I seem to remember doing this using the C standard library in the dim and distant past, but can I do this using the C++ library?!! :mad: I've tried with the <fstream> library, and seekp() & tellp() but am getting a bit unstuck :~ Any ideas anyone?

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      Open the file using CreateFile. Now use SetFilePointer with the FILE_END flag. Now call SetFilePointer with FILE_CURRENT and use -8 as the lDistanceToMove value. Thus it moves back 8 bytes (</log>\r\n). If you have only a \n, then put -7 as lDistanceToMove. Now write your stuff and close the file. Nish


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

      L 1 Reply Last reply
      0
      • N Nish Nishant

        Open the file using CreateFile. Now use SetFilePointer with the FILE_END flag. Now call SetFilePointer with FILE_CURRENT and use -8 as the lDistanceToMove value. Thus it moves back 8 bytes (</log>\r\n). If you have only a \n, then put -7 as lDistanceToMove. Now write your stuff and close the file. Nish


        Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

        L Offline
        L Offline
        l a u r e n
        wrote on last edited by
        #3

        i think a slightly more robust way would be to read the last 100 bytes into memory and find the string and calc its offset and use that when seeking the file position ... unicode chars and whatnots might screw u up if u hard code the sizes into the algorithm


        "... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
        sonork: 100.18128   8028finder.com

        1 Reply Last reply
        0
        • J Jon Taylor

          I'm working on an app that dumps loads of trace information to a file, as XML: <log> <message> .... </message> </log> Now, when the app closes I write "</log>" as the last line. However, the next time the app is run, I want to append to this file, which means I must remove that last line. (It's the XML "root node" :) ) Now, I know I can rename the file, re-parse it and re-write it etc but these log files reach 10's of Mb in size, so it takes too long to do that. So I want to over-write the last line with some new text instead. I seem to remember doing this using the C standard library in the dim and distant past, but can I do this using the C++ library?!! :mad: I've tried with the <fstream> library, and seekp() & tellp() but am getting a bit unstuck :~ Any ideas anyone?

          S Offline
          S Offline
          Sharad Ganesh
          wrote on last edited by
          #4

          hi, If you're keen on using the C++ library, u could do this with fstream's ::seekg to seek to the end(ios::end) minus the offset which is calculated. The offset would be calculated using sizeof(string). Specify the offset in (-)ve. Now you can write the present contents from this location. P.S. Open the file in binary mode. hope this helps, Sharad In C you write your own bugs, in C++ you inherit them ! - Anonymous

          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