tags:

views:

3234

answers:

6

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  A: 

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

open("file", O_TRUNC, O_WRONLY);
pixelbeat
+2  A: 

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.
DeadHead
This is a very inefficient way to perform this. @Jonathan Leffler's method is much preferable, and if your platform doesn't allow it, at least do a write/rename cycle, to prevent dataloss in the event of a crash (copy data to new file under temp name, then swap the names (atomicly if possible), then delete the old file)
derobert
+4  A: 

<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);
gabehabe
+7  A: 

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.

Jonathan Leffler
Two minutes ahead... nice!
Lance Richardson
Remarkably similar, Lance! At least one of us probably got the info correct. (Using <sys/types.h> is not supposed to be necessary in the more recent POSIX standards; for safety, it does no harm, though.)
Jonathan Leffler
Is there such function in c++ vs 2003 I couldn't find it?non of the includes helped maybe I missed something?I do not want to open the file with w but use the file opended already
sofr
I'm not sure. Most of the POSIX functions are available in MSVC, but Microsoft decided that it needed to put an underscore at the start of the function name -- hence _truncate() and _ftruncate(). Also, these are C functions, not C++; you may need to track it down that way. In the MSVC 2008 I have, I don't find a unistd.h header; I'm not sure whether that is available in other editions, or whether I've simply not looked in the right place.
Jonathan Leffler
`ftruncate()` can also be used to increase the size of a file.
0x6adb015
@0x6adb015: yes, that's why I mentioned it in my answer.
Jonathan Leffler
+2  A: 

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.

Lance Richardson
+2  A: 

in windows systems there's no lib but yet you can truncate a file by using

_chsize( fileno(f), size);

sofr