What is a proper method of opening a file for writing as binary file?
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What is a proper method of opening a file for writing as binary file?
Pick ONE option
Show answer & explanation
In C, fopen() is the standard function to open files. The mode string "wb" means open for writing in binary mode—the "w" opens for write access, and the "b" specifies binary mode. Option A misuses fwrite() (which writes data, not opens files) with an invalid mode. Option B opens in text mode, not binary. Option D calls a non-existent function fwriteb().
Step-by-step Derivation:
The C standard library provides fopen(filename, mode) to open files. Valid mode strings are: "r" (read text), "w" (write text), "rb" (read binary), "wb" (write binary), etc. To open for binary write, we need both the write operation ("w") and binary designation ("b"), yielding "wb". Therefore, fopen("test.bin", "wb") is correct. fwrite() and fwriteb() are for writing data to already-opened file pointers, not for opening files.