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?
7 Answers
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.
8 Comments
ftruncate(file no(fp)) on an open file. Neither the internal read nor write buffers of fp will be flushed. Call std::fflush before to flush the write buffers and std::freopen afterwards to reinitialise fpstd::fflush or std::freopen in C; you must be confusing this with a different language — presumably C++. It depends on what you mean by 'is not safe'. Certainly, you're immediately stepping outside the knowledge of the file stream, doing operations behind its back. Nothing is going to flush buffers; you should use fflush(fp) before using ftruncate(fileno(fp)). What happens next depends on how you opened the file, on whether you reposition the file stream before the next operation, and other details. At the o/s level, though, the file will have been truncated.In Windows systems there's no header <unistd.h> but yet you can truncate a file by using
FILE *f = ...;
_chsize( fileno(f), size);
7 Comments
fileno() is standardized in POSIX. Microsoft's "deprecation" is curious, at best.That's a function of your operating system. The standard POSIX way to do it is:
open("file", O_TRUNC | O_WRONLY);
5 Comments
truncate("file", 0); to set its length to zero. It's a POSIX function. But yeah, if you want the file open afterwards, like a shell foo > file redirect would, this is what you want.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.
Comments
<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);
2 Comments
"w" or "w+" will truncate the file. man7.org/linux/man-pages/man3/fopen.3.html ("w+" opens for read+write, so you can write, seek, and read what you wrote. For write-only, omit the +). To open with the ability to write but without truncating, use "a" or "a+" (append) or "r+".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 = fopen("filename.txt", "r");
//read in the data you want to keep
fclose(inFile);
FILE *outFile = fopen("filename.txt", "w");
//output back the data you want to keep into the file, or what you want to output.
fclose(outFile);
2 Comments
In Windows if you just want to truncate the file after you've finished writing then SetEndOfFile() is the way to go for a HANDLE. If you are working with an fd then SetEndOfFile((HANDLE) _get_osfhandle(fd)). _chsize() or _ftruncate() may only support 32 bit arguments on 32 bit compilers. In Unix AFAIK there isn't any "no size" function like SetEndOfFile().