Meaning of mode string in fopen: In the call fp = fopen("source.txt", "rb"); what does the mode "rb" instruct the C library to do?

Difficulty: Easy

Correct Answer: open "source.txt" in binary mode for reading

Explanation:


Introduction / Context:
The fopen mode string controls how a file is opened—text vs binary, reading vs writing, appending, and whether a new file is created or an existing one is truncated. Choosing the correct mode avoids data loss and platform-specific newline translations.


Given Data / Assumptions:

  • Call shown: fopen("source.txt", "rb").
  • Standard C fopen modes apply.
  • We aim to interpret the two-character mode "rb".


Concept / Approach:

  • "r" means open existing file for reading.
  • "b" means binary mode (no text translations such as CRLF to LF on Windows).
  • To read and write, modes like "rb+" or "r+b" are used; to create/truncate for writing, use "w" variants.


Step-by-Step Solution:

Parse "r" → open for reading only.Parse "b" → open in binary mode (no newline conversion).Therefore, "rb" means open in binary mode for reading.


Verification / Alternative check:

If the intent were read/write, "rb+" would be required; if write/truncate, "wb" would be used.


Why Other Options Are Wrong:

  • Binary read/write: Requires "+" in the mode.
  • Create for read/write: That is "wb+" or "w+b", not "rb".
  • None of above: Incorrect because option A is accurate.


Common Pitfalls:

  • Opening text files in text mode on Windows and being surprised by newline translation; use "b" for binary data.
  • Forgetting that "r" fails if the file does not exist.


Final Answer:

open "source.txt" in binary mode for reading

More Questions from Input / Output

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion