-
EOF/File size reduction
Hi,
I am looking to remove certain sections (ranges of bytes) from files. I used fseek/fwrite/fread to essentially "compact" the data by overwriting the data I was removing with what came after it. After this procedure, there is data on the end of the file which doesn't belong. I need to somehow "truncate" my file at a certain point. I heard you could write EOF to the file and the OS would handle discarding the stuff after, thus giving the desired effect. I actually need the file to shrink by the amount of bytes I desired to remove. However, I haven't found any way to do this with the C or C++ libraries. Anyone able to help me?
-
Normal file reading and writing cannot make a file shorter (except by starting a new file and just copying the bits you need)
Various non-standard functions exist which can do what you want, for example
http://man.he.net/?topic=truncate§ion=2
-
Maybe I get you wrong, but in case your intention is saving storage space, you should also consider clustering.
Depending on the OS - more precisely file system type, the space *really* needed by a file is rounded up to the smallest allocation unit provided by the given file system. Therefore you might be struggling to save a few bytes, but there's you end up saving nothing...