cfiletruncatetruncation

How to truncate a file in C?


I'm using C to write some data to a file. I want to erase the previous text written in the file in case it was longer than what I'm writing now. I want to decrease the size of file or truncate until the end. How can I do this?


Solution

  • In Windows systems there's no header <unistd.h> but yet you can truncate a file by using

    FILE *f = ...;
    _chsize( fileno(f), size);