Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I delete text from a file using C++?

Status
Not open for further replies.

puck71

Technical User
Jun 19, 2003
2
0
0
US
It seems like this should be a simple task, but I have been unsuccessful so far. I want to read a line of text from a file, and when I'm done using it I want to delete it from the file. I know I could do something like copy the input file to a new output file just without the part I want deleted, or keep track of where I last read from the file and seek there every time, but I don't want to do either of those. I just want to read a line of text from a file, use it, then delete the text. The file will be relatively large, so any other solution would be inefficient I think. I don't care if I use fstream stuff or fopen stuff, just something that works and I can understand.

Thanks
John
 
John, you seem to have the idea. Your analysis is correct accept at the very end where you think there is another way. There is not. A file is a collection of bytes on the disk, there is no magic way to remove bytes from the middle and slide the existing bytes back to fill the hole just created.

If you really must not re-write the remainder of the file (this is a common technique) then you can use formatting information in the file to tell your software that that section is deleted. Of course only your software will be able to read/write the file.


-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top