0

I have a simple C program that counts how many seconds has passed then writes that number to a File. Basic stuff, but every time I run it the previous number let’s say 15 seconds is replaced with the new one. How do I go about being able to run that program whenever and it records all the instances of those seconds and not just delete the previous and insert a new. Basically I’m recording static dry breath holds for free diving and want to see the progress in a week/month/etc. So I need the data written to the file the first day to stay there. Thanks guys

1 Answers1

1

You need to open the file in append mode. This way your file won't be rewritten each time but instead new content will be added to the end of the file.

Let's say that you have this:

FILE *stackoverflow;

stackoverflow=fopen("myfile.dat", "a"); /* here opening file in "a" will resolve the issue */
if(stackoverflow==NULL) {
    perror("Error opening file.");
}
else {
    /* do as you wish so */
    fclose(stackoverflow);
}