C Programming - Input / Output - Discussion

2. 

What is the purpose of "rb" in fopen() function used below in the code?

FILE *fp;
fp = fopen("source.txt", "rb");

[A]. open "source.txt" in binary mode for reading
[B]. open "source.txt" in binary mode for reading and writing
[C]. Create a new file "source.txt" for reading and writing
[D]. None of above

Answer: Option A

Explanation:

The file source.txt will be opened in the binary mode.


Kushbu said: (Feb 4, 2011)  
rb = read in binary mode so the answer is A.

Pruthvi said: (Jul 22, 2011)  
What is binary mode ?

Vinod Thete said: (Dec 15, 2011)  
What is rb?

Sushma said: (Jul 17, 2012)  
So what exactly is the difference between text and binary modes? Well the difference is that text files contain lines (or records) of text and each of these has an end-of-line marker automatically appended to the end of it whenever you indicate that you have reached the end of a line. There is an end of line at the end of the text written with the C fwrite() function or in C++ when you <. Binary files are not broken up into separate lines or records so the end-of line marker is not written when writing to a binary file.

Reading from a text file or binary file is different too as a text file is automatically broken up into separate records as it read in based on the location of the end-of-line markers.

Prathyusha said: (Nov 4, 2014)  
RB-means read in binary mode.

Prathyusha said: (Nov 4, 2014)  
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 idiosyncrasies of the local operating system. For example, you will know that new lines 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.

Divya said: (May 25, 2017)  
Please, can anyone explain how it will read in binary mode?

Deep said: (Sep 5, 2017)  
If in my .txt file contains "Hello \n world". I want to open this file in binary read mode so whats the output? Please tell me.

Mohit said: (Aug 29, 2018)  
Thanks for explaining it.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.