Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When modifying a file, is it always necessary to rewrite the entire file?

Tags:

c#

file-io

When modifying a file, is it always necessary to rewrite the entire file or is it possible to find and change some small part of a file without having to rewrite the whole thing?

like image 471
Brandon Moore Avatar asked Feb 02 '26 12:02

Brandon Moore


1 Answers

If you're not inserting or deleting data, then you don't need to rewrite the file. You will be replacing existing bytes with new values, or appending to the file.

If you need to insert or delete, you only need to rewrite the file from that point onwards. The only time you would need to rewrite the entire file is if you are inserting bytes at the beginning.

It's okay to open a file with both read and write permissions. That way you can search the file for whatever you're looking for, and once you have the position you can seek to it (from memory, the write pointer is separate from the read pointer) and overwrite data to your heart's content =)

like image 138
paddy Avatar answered Feb 05 '26 01:02

paddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!