What is the difference between "rb+" and "wb+"?
13,217
In short
rb+
does not create the file from scratchwb+
does create the file from scratch
there are no differences aside that.
Related videos on Youtube
Author by
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
rb+
andwb+
both read from and write to a binary file, so what makes them different?Is it the order they read and write?
-
Jon Clements over 5 yearsDo you mean apart from the fact that opening in read only denies being able to write?
-
user4815162342 over 5 years@JonClements Neither of these are read-only,
+
means "open a disk file for updating (reading and writing)". -
Jon Clements over 5 years@user4815162342 ahh... yes... of course... that's quite an embarrassing brain burp of mine :)
-
-
user4815162342 over 5 yearsIt's important to realize that "create the file from scratch" implies "destroy (truncate to length 0) existing one, if found".
-
Admin over 5 yearsOh so with
rb+
the file must already exist? -
Zeromika over 5 years@coder80 Indeed file must already exist while using
rb+
. -
user4815162342 over 5 years@coder80 Yes. Sadly, there is no mode for "use the file if it exists, create one if not". (Other than
r
andw
, there is alsox
which is like the opposite ofr
, in that it will create the file if it doesn't exist, but it will fail if it exists.)