4

I tried to use both of them opening a file but got the same result.

w+ is supposed to create a new file if it doesn't exist while r+ not. Is this the only difference?

For example, this is an initial file file.txt

Line 1
Line 2
Line 3

1) file.txt after implementing of r+ mode (writing the word "hello")

hello

2) file.txt after implementing of w+ mode

hello

I guessed that r+ would append an input to the beginning of the file.

EDITED: So, the 1st difference is about creating a file that doesn't exist and the 2nd difference is that r+ overwrites while w+ truncates, right? (than the question arises whether overwriting and truncating are the same concepts or not)

Nikita Shub
  • 109
  • 2
  • 8

1 Answers1

3

Remove file.txt first and you'll understand the difference.

w+ will create the file.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124