tags:

views:

114

answers:

5

Hello guys,

I would like to know how the contents of a file can be cleared in c. I know it can be done using truncate but I dont find any source clearly describing it.

Thanks & Regards,
Mousey

+10  A: 

The other answers explain how to use truncate correctly... but if you found yourself on a non-POSIX system that doesn't have unistd.h, then the easiest thing to do is just open the file for writing and immediately close it:

#include <stdio.h>

int main()
{
    FILE *file = fopen("asdf.txt", "w");
    if (!file)
    {
        perror("Could not open file");
    }

    fclose(file);
    return 0;
}

Opening a file with "w" (for write mode) "empties" the file so you can start overwriting it; immediately closing it then results in a 0-length file.

Mark Rushakoff
`if (!file) perror("open");`
Greg Bacon
To be completely safe, check fclose return value also ;)
Nyan
+5  A: 

The truncate() call in UNIX is simply:

truncate("/some/path/file", 0);
caf
+3  A: 

While you can just open and close the file, the truncate call is designed specifically for this use case:

#include <unistd.h> //for truncate
#include <stdio.h>  //for perror

int main()
{
    if (truncate("/home/fmark/file.txt", 0) == -1){
      perror("Could not truncate")
    }
    return 0;
}

If you already have the file open, you can use that handle with ftruncate:

#include <stdio.h>  //for fopen, perror
#include <unistd.h> //for ftruncate

int main()
{
    FILE *file = fopen("asdf.txt", "r+");
    if (file == NULL) {
        perror("could not open file");
    }

    //do something with the contents of file

    if (ftruncate(file, 0) == -1){
          perror("Could not truncate")
    }

    fclose(file);
    return 0;
}
fmark
pointer(pointer to FILE) comparison with integer?
Nyan
Ack, good catch!
fmark
`ftruncate` does not take a `FILE *` argument. Should be `ftruncate(fileno(file),0)`.. but I question the wisdom of truncating a file while you have it open for use with stdio functions. If your program uses stdio rather than POSIX file descriptor functions you should probably just close and reopen the file in `"w"` mode to truncate it.
R..
+1  A: 

For deleting the contents of a fie obviously there is basic method of opening a file in write mode "w" and then close it without doing any changes in it.

FILE *fp = fopen (file_path, "w");

fclose(fp);

this will delete all the data in file as when you open a already existing file using "w" mode the file is deleted and a new file with the same name is opened for writing, this will result into deletion of contents of your file.

BUT there is truncate syscall in UNIX systems, which is specially for the same purpose and pretty easy to use:

truncate (filepath, 0);

if you have already opened your file so either you close your file before doing truncate or use ftruncate

ftruncate (file_path, 0);
Kumar Alok
@ Natahn, Thanks bro for editing answer, Now I know how to put a code block in answer. Thanks.
Kumar Alok
Calling the variable `file_path` with `ftruncate` is rather misleading. It should be called `fd` or such..
R..
+1  A: 

truncate(2) is not a portable call. It only conforms to 4.2BSD. While it is found on most *nix type systems, I would say use a POSIX.1 compliant routines which are pretty much guaranteed on most modern environments (including Windows).

so here is a POSIX.1-2000 compliant code snippet:

int truncate_file(const char *name) {
    int fd;
    fd = open (name, O_TRUNC|O_WRONLY);
    if ( fd >= 0 ) 
        close(fd); /* open can return 0 as a valid descriptor */
    return fd;
}
Elf King
Wrong, it's been in XSI options for a long time, and as of POSIX 2008 it's in base. See http://www.opengroup.org/onlinepubs/9699919799/functions/truncate.html
R..