How to truncate a file in C?

CFileTruncateTruncation

C Problem Overview


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?

C Solutions


Solution 1 - C

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.

Solution 2 - C

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

 _chsize( fileno(f), size);

Solution 3 - C

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

open("file", O_TRUNC | O_WRONLY);

Solution 4 - C

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.

Solution 5 - C

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.

Solution 6 - C

<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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsofrView Question on Stackoverflow
Solution 1 - CJonathan LefflerView Answer on Stackoverflow
Solution 2 - CsofrView Answer on Stackoverflow
Solution 3 - CpixelbeatView Answer on Stackoverflow
Solution 4 - CLance RichardsonView Answer on Stackoverflow
Solution 5 - CDeadHeadView Answer on Stackoverflow
Solution 6 - CDanny BattisonView Answer on Stackoverflow