开发者

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

开发者 https://www.devze.com 2022-12-24 11:39 出处:网络
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:

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();

   开发者_StackOverflow社区 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.


This is how you should really do it. This way you only need to open the file once, and it will be truncated when you do. Note that you'll have to change your function prototype as well (wherever it is).

int main()
{
    int i;

    FILE *data;
    data = fopen("output.dat", "w");
    if(!data) {
         perror("Error opening file");
         return 1;
    }

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

    fclose(data);

    return 0;
}

void writedata(FILE *data)
{

    /* do other stuff */
    ...
}


I don't understand why you need it this way but it seems ftruncate() POSIX function is what you are looking for.


Why are you opening the same file over and over again?

Why not open it outside the writedata function with the w+ mode and pass the file pointer to writedata so you can /* do other stuff */ with it?


How about this way? Not quite as neat as some of the other solutions.

#include<stdio.h> 
static int t = 0;    
void writedata()
{
   FILE* data;
   if(t == 0) {
      data = fopen("output.dat", "w");
      t++;
   } else {
      data = fopen("output.dat", "a");
   }
} 

int main()
{
    int i;
    for(i = 0; i < 100; i++) writedata();
    return 0;
}


Perhaps like this:

static void writedata(int append);

int main()
{
    int i;

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

    return 0;
}

static void writedata(int append)
{
    /* Create file for displaying output */
    FILE *data;
    data = fopen("output.dat", append ? "a" : "w");

    /* do other stuff */
    ...
}

This simply passes in a flag that tells the function whether it should open the file for append or not.

A better solution would be to slighly refactor the code, to just open the file once before the loop, and pass the FILE * into the function. That way, it doesn't have to know if it's appending to an existing file or not.


How about this way:

void clear_file(const char *filename)
{ 
    FILE *output = fopen(filename, "w");
    fclose(output);
}

So file is opened for writing and automatically erased.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号