RandomEli
RandomEli

Reputation: 1557

How to write to a file multiple times in C?

I am writing a C file to that can output the performance time into a csv file. And I also want to write the results to the same csv file every time I run the executable. I'm using a trivial I/O format, and I'll pass in the same output file name with command line.

int main(int argc, char**argv) {
    outFile = fopen(fileName,"w+");
    fprintf(outFile, "%d, %f\n",numbers,time_spent);
    fclose(outFile);
    return 0;
}

Everytime I open the file, fopen() will delete the current content. Is there a way that I can open the file without delete the content?

Upvotes: 0

Views: 1285

Answers (1)

eddiem
eddiem

Reputation: 1030

Use fopen(fileName, "a+"); instead.

Upvotes: 1

Related Questions