4

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?

1 Answers1

11

In short

  • rb+ does not create the file from scratch

  • wb+ does create the file from scratch

there are no differences aside that.

Zeromika
  • 347
  • 2
  • 12
  • 3
    It's important to realize that "create the file from scratch" implies "destroy (truncate to length 0) existing one, if found". – user4815162342 Sep 03 '18 at 11:16
  • Oh so with `rb+` the file must already exist? –  Sep 03 '18 at 11:17
  • 1
    @coder80 Indeed file must already exist while using `rb+`. – Zeromika Sep 03 '18 at 11:21
  • 3
    @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.) – user4815162342 Sep 03 '18 at 11:29
  • One would think ab+ should do that, but it does not – dashesy Mar 12 '23 at 20:05