-
inserting EOF in file
Hello.
I am trying to delete some data from a file i opened. i thought that if i inserted the ascii code of eof before the data i wanted to erase the new end of file would be there. Unfortunately, this didn't work.
Why did this technique failed to work, and any other way i could delete some data from a file?
Thanks in advance.
-
There's no such thing as an EOF ascii code. EOF is just a numerical value (independent of the ascii encoding and system-dependent) returned by your read() function to indicate that there's nothing more to read in the file. To delete some data from a file, I don't think there another way (or at least a portable way) than deleting it completely (e.g. truncate option for open()) and rewritting it. But someone may have a better idea.
-
The portable way is to create a second file, then copy data from the first into the second until you've copied the number of bytes you want. Then remove the original and rename the new one back to the original name.
The unportable way is to call truncate() on POSIX-like systems or SetEndOfFile() on Windows to drop the data directly from the end of the file.