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?
That's a function of your operating system. The standard POSIX way to do it is:
open("file", O_TRUNC, O_WRONLY);
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);
If you want to preserve the previous contents of the file up to some length (a length bigger than zero, which the 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...
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.
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.
in windows systems there's no lib but yet you can truncate a file by using
_chsize( fileno(f), size);