3

How can I delete few characters from a file using C program?

I could not find any predefined functions 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

5 Answers 5

4

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.

1
  • Although be aware that standard C doesn't have a function to truncate a file (other than to 0 length). POSIX does, as does Windows. Nov 20, 2010 at 23:44
1

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.

13
  • I was thinking the same, but, what if the client(which is sending the file) crashes?
    – Blacklabel
    Nov 20, 2010 at 23:10
  • 1
    What if the power is cut before hard disk buffers are flushed? Same problem. What if the power is cut while you are deleting the bytes from the beginning of the file? The file data gets corrupted. You are better off asking the server you are sending to how many bytes it received and send from that point on.
    – cdhowie
    Nov 20, 2010 at 23:12
  • @Blacklabel: you should sync the "current position" from time to time. And maybe the client should include the desired position into the request (like HTTP Content-Range/206)
    – Vlad
    Nov 20, 2010 at 23:15
  • you can use a hand shaking method at the beginning of the connection that client and server tells each other how many bytes server have received and how many byte client have sent, then you should take the minimum of these values as the size of data that transmitted and start sending rest of your data.
    – user415789
    Nov 20, 2010 at 23:15
  • @HPT: The number of bytes the server has received should never be greater than the number the client has sent. The server should always know how many bytes it needs; just take the server value.
    – cdhowie
    Nov 20, 2010 at 23:17
1

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.

1

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.

0

If the char's are one after the other than why dont you give a try to fseek();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.