Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate a file in C?

Tags:

c

file

If you want to preserve the previous contents of the file up to some length (a length bigger than zero, which other answers provide), then POSIX provides the truncate() and ftruncate() functions for the job.

#include <unistd.h>
int ftruncate(int fildes, off_t length);
int truncate(const char *path, off_t length);

The name indicates the primary purpose - shortening a file. But if the specified length is longer than the previous length, the file grows (zero padding) to the new size. Note that ftruncate() works on a file descriptor, not a FILE *; you could use:

if (ftruncate(fileno(fp), new_length) != 0) ...error handling...

However, you should be aware that mixing file stream (FILE *) and file descriptor (int) access to a single file is apt to lead to confusion — see the comments for some of the issues. This should be a last resort.

It is likely, though, that for your purposes, truncate on open is all you need, and for that, the options given by others will be sufficient.


For Windows, there is a function SetEndOfFile() and a related function SetFileValidData() function that can do a similar job, but using a different interface. Basically, you seek to where you want to set the end of file and then call the function.

There's also a function _chsize() as documented in the answer by sofr.


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

 _chsize( fileno(f), size);

That's a function of your operating system. The standard POSIX way to do it is:

open("file", O_TRUNC | O_WRONLY);

If this is to run under some flavor of UNIX, these APIs should be available:

   #include <unistd.h>
   #include <sys/types.h>

   int truncate(const char *path, off_t length);
   int ftruncate(int fd, off_t length);

According to the "man truncate" on my Linux box, these are POSIX-conforming. Note that these calls will actually increase the size of the file (!) if you pass a length greater than the current length.


If you want to truncate the entire file, opening the file up for writing does that for you. Otherwise, you have to open the file for reading, and read the parts of the file you want to keep into a temporary variable, and then output it to wherever you need to.

Truncate entire file:

FILE *file = fopen("filename.txt", "w"); //automatically clears the entire file for you.

Truncate part of the file:

FILE *inFile("filename.txt", "r");
//read in the data you want to keep
fclose(inFile);
FILE *outFile("filename.txt", "w");
//output back the data you want to keep into the file, or what you want to output.

<edit>

Ah, you edited your post, you're using C. When you open the file, open it with the mode "w+" like so, and it will truncate it ready for writing:

FILE* f = fopen("C:\\gabehabe.txt", "w+");
fclose(file);

</edit>

To truncate a file in C++, you can simply create an ofstream object to the file, using ios_base::trunc as the file mode to truncate it, like so:

ofstream x("C:\\gabehabe.txt", ios_base::trunc);