How to replace last line of a file?
-
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?
-
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?
Open the file using
CreateFile
. Now useSetFilePointer
with theFILE_END
flag. Now callSetFilePointer
withFILE_CURRENT
and use -8 as thelDistanceToMove
value. Thus it moves back 8 bytes (</log>\r\n). If you have only a \n, then put -7 aslDistanceToMove
. 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]
-
Open the file using
CreateFile
. Now useSetFilePointer
with theFILE_END
flag. Now callSetFilePointer
withFILE_CURRENT
and use -8 as thelDistanceToMove
value. Thus it moves back 8 bytes (</log>\r\n). If you have only a \n, then put -7 aslDistanceToMove
. 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]
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 -
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?
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