Difference between r+ and w+ in fopen()

CFopen

C Problem Overview


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?

C Solutions


Solution 1 - C

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.

Solution 2 - C

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):

enter image description here

Solution 3 - C

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

Solution 4 - C

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.

Solution 5 - C

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...

Solution 6 - C

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

Solution 7 - C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyaylitzisView Question on Stackoverflow
Solution 1 - CPeterView Answer on Stackoverflow
Solution 2 - ChaccksView Answer on Stackoverflow
Solution 3 - CWaqar NaeemView Answer on Stackoverflow
Solution 4 - Cinvalid_idView Answer on Stackoverflow
Solution 5 - CSathvikView Answer on Stackoverflow
Solution 6 - CperrealView Answer on Stackoverflow
Solution 7 - CAshwani BansalView Answer on Stackoverflow