C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - General Questions (Q.No. 2)
2.
What is the purpose of "rb" in fopen() function used below in the code?
FILE *fp;
fp = fopen("source.txt", "rb");
open "source.txt" in binary mode for reading
open "source.txt" in binary mode for reading and writing
Create a new file "source.txt" for reading and writing
None of above
Answer: Option
Explanation:

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

Discussion:
9 comments Page 1 of 1.

MOHIT said:   6 years ago
Thanks for explaining it.

Deep said:   7 years ago
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.

Divya said:   8 years ago
Please, can anyone explain how it will read in binary mode?

Prathyusha said:   1 decade ago
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.

Prathyusha said:   1 decade ago
RB-means read in binary mode.

Sushma said:   1 decade ago
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.

Vinod thete said:   1 decade ago
What is rb?

Pruthvi said:   1 decade ago
What is binary mode ?

Kushbu said:   1 decade ago
rb = read in binary mode so the answer is A.

Post your comments here:

Your comments will be displayed after verification.