C Programming — File Handling
C language from basics to advanced placement prep
100 Questions 10 Topics Take Test
Advertisement
Showing 81–90 of 100 questions in File Handling
Q.81 Easy File Handling
In file handling, what does the 'b' flag in file modes like 'rb' or 'wb' indicate?
A Binary mode operation
B Buffered mode operation
C Bidirectional mode operation
D Backup mode operation
Correct Answer:  A. Binary mode operation
EXPLANATION

The 'b' flag specifies binary mode, which prevents any translation of line endings or special characters.

Take Test
Q.82 Easy File Handling
Which of the following file modes in fopen() allows reading only?
A "w"
B "r"
C "a"
D "w+"
Correct Answer:  B. "r"
EXPLANATION

The 'r' mode opens a file for reading only. The file must exist, otherwise fopen() returns NULL.

Take Test
Q.83 Easy File Handling
When opening a file in 'w' mode, what happens if the file already exists?
A The file is truncated and all previous content is deleted
B The file is opened in append mode automatically
C An error is returned and file is not opened
D The file pointer is positioned at the end
Correct Answer:  A. The file is truncated and all previous content is deleted
EXPLANATION

Opening a file in 'w' mode truncates the file to zero length if it exists, discarding all previous content.

Take Test
Q.84 Hard File Handling
Which of the following correctly implements appending to a file?
A FILE *fp = fopen("file.txt", "a");
B FILE *fp = fopen("file.txt", "w");
C FILE *fp = fopen("file.txt", "r+");
D FILE *fp = fopen("file.txt", "ab+");
Correct Answer:  A. FILE *fp = fopen("file.txt", "a");
EXPLANATION

Mode "a" opens a file for appending. New data is written at the end of the file. Mode "w" truncates the file, "r+" requires file to exist, "ab+" is appending in binary with both read/write.

Take Test
Q.85 Hard File Handling
Write a code snippet to read 100 bytes from a file. Which is correct?
A fread(buffer, 1, 100, fp);
B fread(buffer, 100, 1, fp);
C fread(buffer, 100, fp);
D fread(100, buffer, fp);
Correct Answer:  A. fread(buffer, 1, 100, fp);
EXPLANATION

fread() syntax is fread(ptr, size, count, stream). To read 100 bytes, size=1 and count=100. Option B reads 100 items of 1 byte each, which is equivalent but less clear.

Take Test
Q.86 Medium File Handling
What happens when fseek() is used with SEEK_END as origin?
A File pointer moves to end of file
B File pointer stays at current position
C File pointer moves to beginning
D File is closed
Correct Answer:  A. File pointer moves to end of file
EXPLANATION

SEEK_END moves the file pointer to the end of the file. Offset can be 0 or negative to move before the end. SEEK_SET starts from beginning, SEEK_CUR from current position.

Take Test
Q.87 Medium File Handling
Consider the code: FILE *fp = fopen("data.txt", "r"); if(fp == NULL) { printf("Error"); } What does this check?
A Whether file opened successfully
B Whether file is empty
C Whether file pointer is valid
D Whether file has read permission
Correct Answer:  A. Whether file opened successfully
EXPLANATION

This code checks if fopen() returned NULL, which indicates the file failed to open. This is the standard error checking method for file operations.

Take Test
Q.88 Medium File Handling
What is the purpose of rewind() function in file handling?
A Moves file pointer to the beginning
B Closes the file
C Reverses file content
D Undoes last write operation
Correct Answer:  A. Moves file pointer to the beginning
EXPLANATION

rewind() moves the file pointer to the beginning of the file and clears the EOF indicator. It's equivalent to fseek(file, 0, SEEK_SET).

Take Test
Q.89 Medium File Handling
What does fwrite() function return?
A Number of items successfully written
B Number of bytes written
C 1 on success, 0 on failure
D Always returns 0
Correct Answer:  A. Number of items successfully written
EXPLANATION

fwrite() returns the number of items successfully written to the file, which may be less than the count requested if an error occurs.

Take Test
Q.90 Medium File Handling
Which mode allows both reading and writing without truncating?
A "r+"
B "w+"
C "a+"
D "rb+"
Correct Answer:  A. "r+"
EXPLANATION

"r+" mode opens a file for both reading and writing without truncating it. The file must exist. "w+" creates a new file or truncates existing one.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips