How does one shrink a file in C? For example, if I open a 2mb file, do some work and decide only 1mb of the file should still exist, how is this accomplished? I know how to use write( fd,….) to write to a file, but what if I want to carve out certain parts in the middle of a file or truncate it?
Share
You’re likely looking for
truncate()ftruncate()or on windows,SetEndOfFile(). There’s no way to “cut” out a piece of the file, you will need to rewrite it from front and then truncate it.