What is the difference between "rb+" and "wb+"?

13,217

In short

  • rb+ does not create the file from scratch

  • wb+ does create the file from scratch

there are no differences aside that.

Share:
13,217

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    rb+ and wb+ both read from and write to a binary file, so what makes them different?

    Is it the order they read and write?

    • Jon Clements
      Jon Clements over 5 years
      Do you mean apart from the fact that opening in read only denies being able to write?
    • user4815162342
      user4815162342 over 5 years
      @JonClements Neither of these are read-only, + means "open a disk file for updating (reading and writing)".
    • Jon Clements
      Jon Clements over 5 years
      @user4815162342 ahh... yes... of course... that's quite an embarrassing brain burp of mine :)
  • user4815162342
    user4815162342 over 5 years
    It's important to realize that "create the file from scratch" implies "destroy (truncate to length 0) existing one, if found".
  • Admin
    Admin over 5 years
    Oh so with rb+ the file must already exist?
  • Zeromika
    Zeromika over 5 years
    @coder80 Indeed file must already exist while using rb+.
  • user4815162342
    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 and w, there is also x which is like the opposite of r, in that it will create the file if it doesn't exist, but it will fail if it exists.)