Difference between r+ and w+ in fopen()
The main difference is w+
truncate the file to zero length if it exists or create a new file if it doesn't. While r+
neither deletes the content nor create a new file if it doesn't exist.
Try these codes and you will understand:
#include <stdio.h>int main(){ FILE *fp; fp = fopen("test.txt", "w+"); fprintf(fp, "This is testing for fprintf...\n"); fputs("This is testing for fputs...\n", fp); fclose(fp);}
and then this
#include <stdio.h>int main(){ FILE *fp; fp = fopen("test.txt", "w+"); fclose(fp);}
If you will open test.txt
, you will see that all data written by the first program has been erased.
Repeat this for r+
and see the result.
Here is the summary of different file modes (1 => ture
, 0 => false
):