Answer the question
In order to leave comments, you need to log in
Is it possible to delete bytes from a file without completely overwriting it only from the end?
Correctly? Or can it also be deleted from the middle without overwriting the file?
If our file weighs gigabytes.
Answer the question
In order to leave comments, you need to log in
You can not overwrite the entire file, but only a piece from the tail, which will remain after deletion from the middle of the data. To trim the file size from the end, modern operating systems have appropriate mechanisms.
If you need to delete a few bytes at the beginning of a large file, you will have to overwrite the entire file.
In order not to overwrite large amounts of information, many files inside have a block structure (for example, database files), then you can mark the entire block as deleted, while the block itself from the file is not freed and not overwritten, but the application knows that it is empty and it can be reused if necessary. You must program this functionality in your application yourself - the operating system will not do it for you. In this case, when the evil admin shrinks the database, these empty blocks will be physically removed from the database.
Block writing to a disk and copy-on-write are all true, but they are at the disk driver level (and copy-on-write is not even a driver at all, but the firmware of the disk controller). At the same time, the disk driver and disk controller do not know anything about files, they work with sectors, blocks, etc.
The user program, when working with files, usually does not go down to such a low level, and therefore cannot manage such operations.
Philosophically, everything is cool .. but where are the examples of C # code then?
I do not know a way without rewriting the file (if the OS can optimize - praise it upd but .. name it)))
and the basic set of information, for example here - https://metanit.com/sharp/tutorial/5.4.php
ps in modern computers, with file sizes of a few megabytes, these are all trifles
pps
- synchronization by blocks / clusters is needed, in theory - yes, no problem, just not C #
ppps
at best, these will be system api calls, but then I repeat - examples into the studio and I'll join in the applause
In general, it depends on the file system.
In most known systems, it makes no difference - at least from the beginning, at least from the end, delete, no one will completely overwrite the file.
The same is with adding - you can add at least a gigabyte in the middle of the file.
Why touch the file itself?
Basically, the principle is this
- Writing to disk goes in blocks of a fixed size - clusters.
This is the minimum addressable unit of the file system, the cluster size can be different, most often 4 kilobytes.
You have changed part of the file - clusters that have changed will be written to the disk in any free space. The file location coordinates will change in the file system database.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question