Delete a character from a file in C
How can I delete few characters from a file using C program?
I could not find any predefined func开发者_运维知识库tions for it.
To understand the purpose, I am trying to send a file through a socket, if N bytes are sent successfully, I want to delete those bytes from the file. At the end, the file will be empty. Any other way to do this efficiently?
Thanks Pradeep
If they're at the end, truncate the file at the appropriate length. If they're not then you'll need to rewrite the file.
Your way is pretty inefficient for large files, since you would have to copy "the rest of the file" some bytes further to the beginning, which costs much. I would rather record the "current sending position" somewhere outside of the file and update that information. That way, you don't have to copy the rest of the file so often.
There is no straightforward way to delete bytes from the beginning of a file. You will have to start from where you want to trim the file, and read from there to the end of the file, writing to the start of the file.
It might make more sense to just track how many bytes you have already written to the file in some other file.
you should use an index which points to the beginning of the data you haven't sent yet. It is not necessary to delete what you have sent, just pass them, when you send the whole file delete it.
If the char's are one after the other than why dont you give a try to fseek();
精彩评论