How do I clear the contents of a file using c?

Posted by Eddy on Stack Overflow See other posts from Stack Overflow or by Eddy
Published on 2010-03-18T17:12:26Z Indexed on 2010/03/18 18:01 UTC
Read the original article Hit count: 228

Filed under:
|
|

I'm writing some code so that at each iteration of a for loop it runs a functions which writes data into a file, like this:

int main()
{
    int i;

    /* Write data to file 100 times */
    for(i = 0; i < 100; i++)    writedata();

    return 0;
}

void writedata()
{
    /* Create file for displaying output */
    FILE *data;
    data = fopen("output.dat", "a");

    /* do other stuff */
    ...
}

How do I get it so that when I run the program it will delete the file contents at the beginning of the program, but after that it will append data to the file? I know that using the "w" identifier in fopen() will open a new file that's empty, but I want to be able to 'append' data to the file each time it goes through the 'writedata()' function, hence the use of the "a" identifier.

© Stack Overflow or respective owner

Related posts about c

    Related posts about file