Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the differences between r and rb in fopen

I tried using fopen in C, the second parameter is the open mode. The two modes "r" and "rb" tend to confuse me a lot. It seems they are the same. But sometimes it is better to use "rb". So, why does "r" exist? Explain it to me in detail or with examples. Thank You.

like image 920
Shaobo Wang Avatar asked Feb 01 '10 05:02

Shaobo Wang


People also ask

What does mode RB mean?

Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing. Unlike text files, binary files are not human-readable.

What does RB mean in C programming?

rb. Open for reading in binary mode. If the file does not exist, fopen() returns NULL. w. Open for writing.

What is the difference between the file opening modes R +' and W +'?

a - opens a file in append mode. r+ - opens a file in both read and write mode. a+ - opens a file in both read and write mode. w+ - opens a file in both read and write mode.


1 Answers

You should use "r" for opening text files. Different operating systems have slightly different ways of storing text, and this will perform the correct translations so that you don't need to know about the idiosyncracies of the local operating system. For example, you will know that newlines will always appear as a simple "\n", regardless of where the code runs.

You should use "rb" if you're opening non-text files, because in this case, the translations are not appropriate.

like image 68
caf Avatar answered Oct 09 '22 03:10

caf