tags:

views:

320

answers:

5

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.

A: 

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.

unwind
+4  A: 

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?

Nick Presta
Sorry I'm a bit of a newb, how do I pass the file pointer to the writedata function? I've tried it before but I couldn't figure it out
Eddy
Ok I've figured it out now, I had to use "w" not "w+" otherwise I get a segmentation fault.
Eddy
+1 for noting how insane it is to open a file over and over again, which is an extremely expensive operation.
Michael Aaron Safyan
+2  A: 

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

qrdl
+5  A: 

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 */
    ...
}
jdizzle
+1 for showing how to call fopen before loop
Jack
Also note that there is a system call open() which also does the job. I think fopen is more appropriate here.
Jack
+1  A: 

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;
}
Lazer