It says in both r+ and w+ we can read and write a file.so in what way r+ and w+ are different??
In what file modes Can we write in a middle of a file.Here i want to overwrite the content of a file at particular position and not inserting.
It says in both r+ and w+ we can read and write a file.so in what way r+ and w+ are different??
In what file modes Can we write in a middle of a file.Here i want to overwrite the content of a file at particular position and not inserting.
r
open text file for reading
w
create text file for writing; discard previous contents if any
a
append; open or create text file for writing at end of file
r+
open text file for update (i.e., reading and writing)
w+
create text file for update; discard previous contents of any
a+
append; open or create text file for update, writing at end
in both w
and w+
modes u can write using fseek. Note that w+ creates a file if it doesn't exists!
The w+
mode will truncate (empty) the file. So if you want to change the contents rather than write a new file, use r+
. Note that, as Tim Cooper noted, you cannot append in the middle of the file, so you can overwite certain parts using fseek
and the write functions. But you cannot insert some text in the middle without rewriting everything that comes after it.