Skip to content
Advertisement

What is the difference between read+write and read+write+truncate?

This is an easy question, but I couldn’t find an explanation anywhere

I’m using fopen in C++, and I have the option to either use

“r+b” (Read and Write)

Or

w+b” (Read Write and Truncate)

What is the meaning of Truncate? What is the difference between “r+b” and “w+b”? I understand the mathematical meaning of truncate is removing precision after a certain number of decimal points, but I don’t understand the exact meaning in the C++ context.

Thanks,

Advertisement

Answer

“truncate” means that the file will be set to size 0 on opening.

"w+b" will give you an empty file (creating it if it didn’t exist) that you can read and write to.

"r+b" will fail if the file does not exist; and if the file does exist then the file remains unchanged for now and you can read or write to it.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement