90

In fopen("myfile", "r+") what is the difference between the "r+" and "w+" open mode? I read this:

"r" Open a text file for reading.
"w" Open a text file for writing, truncating an an existing file to zero length, or creating the file if it does not exist.

"r+" Open a text file for update (that is, for both reading and writing).
"w+" Open a text file for update (reading and writing), first truncating the file to zero length if it exists or creating the file if it does not exist.

I mean the difference is that if I open the file with "w+", the file will be erased first?

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
yaylitzis
  • 5,354
  • 17
  • 62
  • 107
  • 1
    the 2 differences are right there: truncating and creating. –  Jan 14 '14 at 12:50
  • 2
    ... it will be "erased" first, or created if it does not exist (while `r+` would give error in this case. – ShinTakezou Jan 14 '14 at 12:50
  • Does this answer your question? [Difference between modes a, a+, w, w+, and r+ in built-in open function?](https://stackoverflow.com/questions/1466000/difference-between-modes-a-a-w-w-and-r-in-built-in-open-function) – Yogev Neumann Apr 09 '23 at 22:26

7 Answers7

94

Both r+ and w+ can read and write to a file. However, r+ doesn't delete the content of the file and doesn't create a new file if such file doesn't exist, whereas w+ deletes the content of the file and creates it if it doesn't exist.

Peter
  • 7,020
  • 4
  • 31
  • 51
  • 6
    I was searching for a way to not delete the contents of the file (like r+) but also create a new file if it doesn't exist (like w+). I discovered `open(x, 'a').close(); open(x, 'r+')` – cowlinator May 23 '18 at 02:04
72

This diagram will be faster to read next time. Maybe someone else will find that helpful too. This is clearly explained the difference between. enter image description here

Waqar Naeem
  • 963
  • 6
  • 12
  • 2
    flow chart is the best :D – Srinath Ganesh Dec 19 '20 at 16:19
  • 3
    I like this diagram but the "create new file" scenario is missing which is contained in the accepted answer. – MarcusS Jul 20 '22 at 08:14
  • Just found out that "a" does not allow seeking for writing. Documentation for "a+" states "output is always appended to the end of the file", and so it may hold true for "a". – Anonymous Aug 09 '22 at 14:39
  • So no difference between a and a+ Both are for wrirting bowth are not for truncating both initial position is at end. – Irtza Shahan Jun 17 '23 at 07:55
  • 1
    @Irtza Shahan Not exactly the same, all you said is correct. But, in addition to that, you can also use "a+" to read content of the file. You may think that it makes no sense due to initial position is End, but remember you can move the pointer afterwards. – Ganathor Jul 27 '23 at 12:25
61

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:

enter image description here

haccks
  • 104,019
  • 25
  • 176
  • 264
25
r = read mode only
r+ = read/write mode
w = write mode only
w+ = read/write mode, if the file already exists override it (empty it)

So yes, if the file already exists w+ will erase the file and give you an empty file.

invalid_id
  • 804
  • 8
  • 12
6

w+

#include <stdio.h>
int main()
{
   FILE *fp;
   fp = fopen("test.txt", "w+");  //write and read mode
   fprintf(fp, "This is testing for fprintf...\n"); 

   rewind(fp); //rewind () function moves file pointer position to the beginning of the file.
   char ch;
   while((ch=getc(fp))!=EOF)
   putchar(ch);

   fclose(fp);
}  

output

This is testing for fprintf...

test.txt

This is testing for fprintf...

w and r to form w+

#include <stdio.h>
int main()
{
   FILE *fp;

   fp = fopen("test.txt", "w"); //only write mode
   fprintf(fp, "This is testing for fprintf...\n"); 
   fclose(fp);
   fp = fopen("test.txt", "r");
   char ch;
   while((ch=getc(fp))!=EOF)
   putchar(ch);
   fclose(fp);
}  

output

This is testing for fprintf...

test.txt

This is testing for fprintf...

r+

test.txt

This is testing for fprintf...
#include<stdio.h>
int main()
{
    FILE *fp;
    fp = fopen("test.txt", "r+");  //read and write mode
    char ch;
    while((ch=getc(fp))!=EOF)
    putchar(ch);
    rewind(fp); //rewind () function moves file pointer position to the beginning of the file.
    fprintf(fp, "This is testing for fprintf again...\n");
    fclose(fp);
    return 0;
}

output

This is testing for fprintf...

test.txt

This is testing for fprintf again...

r and w to form r+

test.txt

This is testing for fprintf...
#include<stdio.h>
int main()
{
    FILE *fp;
    fp = fopen("test.txt", "r"); 
    char ch;
    while((ch=getc(fp))!=EOF)
    putchar(ch);
    fclose(fp);

    fp=fopen("test.txt","w");
    fprintf(fp, "This is testing for fprintf again...\n");
    fclose(fp);
    return 0;
}

output

This is testing for fprintf...

test.txt

This is testing for fprintf again...

a+

test.txt

This is testing for fprintf...
#include<stdio.h>
int main()
{
    FILE *fp;
    fp = fopen("test.txt", "a+");  //append and read mode
    char ch;
    while((ch=getc(fp))!=EOF)
    putchar(ch);
    rewind(fp); //rewind () function moves file pointer position to the beginning of the file.
    fprintf(fp, "This is testing for fprintf again...\n");
    fclose(fp);
    return 0;
}

output

This is testing for fprintf...

test.txt

This is testing for fprintf...
This is testing for fprintf again...

a and r to form a+

test.txt

This is testing for fprintf...
#include<stdio.h>
int main()
{
    FILE *fp;
    fp = fopen("test.txt", "a");  //append and read mode
    char ch;
    while((ch=getc(fp))!=EOF)
    putchar(ch);
    fclose(fp);
    fp=fopen("test.txt","r");
    fprintf(fp, "This is testing for fprintf again...\n");
    fclose(fp);
    return 0;
}

output

This is testing for fprintf...

test.txt

This is testing for fprintf...
This is testing for fprintf again...
Sathvik
  • 565
  • 1
  • 7
  • 17
3

There are 2 differences, unlike r+, w+ will:

  • create the file if it does not already exist
  • first truncate it, i.e., will delete its contents
perreal
  • 94,503
  • 21
  • 155
  • 181
0

r+ The existing file is opened to the beginning for both reading and writing. w+ Same as w except both for reading and writing.