Practice 20 File Handling multiple-choice questions designed for CDAC CCAT exam preparation. Click "Show Answer" to reveal the correct option with detailed explanation.
Show Answer & Explanation
Correct Answer: A — fopen()
fopen() is the standard library function used to open a file in C. It returns a FILE pointer.
Show Answer & Explanation
Correct Answer: C — Read and write
The "r+" mode opens a file for both reading and writing. The file must exist.
Show Answer & Explanation
Correct Answer: A — fclose()
fclose() is used to close a file that was opened with fopen().
Show Answer & Explanation
Correct Answer: B — End of file
feof() returns non-zero if the end-of-file indicator is set for the stream.
Show Answer & Explanation
Correct Answer: C — Both a and b
Both putc() and fputc() can be used to write a character to a file. They are essentially the same, with putc() possibly being a macro.
Show Answer & Explanation
Correct Answer: B — Opens for writing (creates/truncates)
The "w" mode opens a file for writing. If the file exists, it is truncated. If it does not exist, a new file is created.
Show Answer & Explanation
Correct Answer: B — fgets()
fgets() reads a line (up to specified number of characters or newline) from a file into a string.
Show Answer & Explanation
Correct Answer: C — NULL
fopen() returns NULL if it fails to open the file, which should always be checked before file operations.
Show Answer & Explanation
Correct Answer: C — Append mode
The "a" mode opens a file for appending. Data is written at the end of the file without truncating existing content.
Show Answer & Explanation
Correct Answer: A — fseek()
fseek() is used to move the file pointer to a specific position. It takes file pointer, offset, and origin as arguments.
Show Answer & Explanation
Correct Answer: B — Current position of file pointer
ftell() returns the current position of the file pointer, which indicates how many bytes from the beginning of the file.
Show Answer & Explanation
Correct Answer: C — Moves pointer to beginning
rewind() moves the file pointer back to the beginning of the file. It is equivalent to fseek(fp, 0, SEEK_SET).
Show Answer & Explanation
Correct Answer: A — SEEK_SET
SEEK_SET is used to seek from the beginning of the file. SEEK_CUR is from current position, SEEK_END is from end.
Show Answer & Explanation
Correct Answer: B — fprintf() is formatted, fputs() is unformatted
fprintf() allows formatted output (like printf) to a file, while fputs() writes a string as-is without formatting.
Show Answer & Explanation
Correct Answer: C — Reads a block of data
fread() reads a specified number of blocks of a given size from a file into a buffer. Used for binary file reading.
Show Answer & Explanation
Correct Answer: B — w+
w+ opens for reading and writing. If file exists, it is truncated. If not, a new file is created.
Show Answer & Explanation
Correct Answer: B — Flush the output buffer
fflush() forces any buffered data to be written to the file immediately, without waiting for the buffer to fill.
Show Answer & Explanation
Correct Answer: A — b
Adding "b" to the mode (like "rb", "wb") opens the file in binary mode, which does not translate newline characters.
Show Answer & Explanation
Correct Answer: A — fwrite()
fwrite() writes a block of data (specified number of elements of specified size) to a file. Used for binary files.
Show Answer & Explanation
Correct Answer: B — Undefined behavior/error
"w" mode is write-only. Trying to read from a file opened in "w" mode results in undefined behavior or error.