C Programming — File Handling
C language from basics to advanced placement prep
100 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 100 questions in File Handling
Q.21 Medium File Handling
A program reads struct records using fread(). After reading 100 records of 64 bytes each, what is the total bytes read if successful?
A 100
B 64
C 6400
D fread() doesn't return bytes, only count
Correct Answer:  C. 6400
EXPLANATION

fread(ptr, 64, 100, fp) reads 100 items of 64 bytes each = 6400 bytes total. fread() returns count of items read, not bytes.

Take Test
Q.22 Medium File Handling
What happens if you call fclose() on an already closed file pointer?
A Returns 0
B Returns EOF and may cause undefined behavior
C Silently succeeds
D Raises compile-time error
Correct Answer:  B. Returns EOF and may cause undefined behavior
EXPLANATION

Calling fclose() on closed file causes undefined behavior and returns EOF. This is a runtime issue, not compile-time.

Take Test
Q.23 Medium File Handling
A program needs to append 500 integers to an existing binary file. Which combination is correct?
A fopen(file, 'ab') with fwrite()
B fopen(file, 'a') with fprintf()
C fopen(file, 'ab') with fprintf()
D fopen(file, 'wb') with fwrite()
Correct Answer:  A. fopen(file, 'ab') with fwrite()
EXPLANATION

Binary append requires 'ab' mode with fwrite(). Option B uses text mode. Option D would overwrite. Option C mixes binary mode with text function.

Take Test
Q.24 Easy File Handling
Which mode opens file in text format with write permission only?
A wb
B w
C wt
D wa
Correct Answer:  B. w
EXPLANATION

'w' mode opens file in text format for writing. 'wb' is binary write. 'wt' is explicitly text write but 'w' is default text mode.

Take Test
Q.25 Easy File Handling
Which function returns the current position of file pointer?
A fgetpos()
B ftell()
C fseek()
D rewind()
Correct Answer:  B. ftell()
EXPLANATION

ftell() returns the current file position as a long integer. fgetpos() stores position in a fpos_t structure. rewind() resets to beginning.

Take Test
Q.26 Easy File Handling
What does fseek(fp, 0, SEEK_END) accomplish?
A Moves pointer to beginning of file
B Moves pointer to end of file
C Moves pointer to current position
D Closes the file pointer
Correct Answer:  B. Moves pointer to end of file
EXPLANATION

SEEK_END moves the file pointer to the end of file. SEEK_SET goes to beginning, SEEK_CUR to current position.

Take Test
Q.27 Easy File Handling
Which file opening mode allows both reading and writing without truncating existing content?
A r+
B w+
C a+
D x+
Correct Answer:  A. r+
EXPLANATION

Mode 'r+' opens file for reading and writing without truncating. 'w+' truncates the file, 'a+' appends, and 'x+' is for exclusive creation.

Take Test
Q.28 Hard File Handling
For reading a configuration file line-by-line where lines may exceed 256 characters, which function is recommended for ISRO/GATE 2024 exams?
A fgets(buffer, sizeof(buffer), fp) with dynamic memory if needed
B fscanf(fp, "%s", buffer) with fixed-size array
C fgetc() in a loop to manually read lines
D fread() to read entire file at once
Correct Answer:  A. fgets(buffer, sizeof(buffer), fp) with dynamic memory if needed
EXPLANATION

fgets() is safest for line reading with size protection. For lines exceeding buffer size, use dynamic allocation or getline(). fscanf() with %s is vulnerable to buffer overflow.

Take Test
Q.29 Hard File Handling
Which scenario would be most problematic when using fprintf() for data that needs exact binary preservation?
A fprintf() interprets format specifiers and may alter binary data representation
B fprintf() is always safer than fwrite()
C fprintf() cannot handle NULL characters in data
D Both A and C are correct
Correct Answer:  D. Both A and C are correct
EXPLANATION

fprintf() is text-oriented and interprets format specifiers, potentially modifying data. It also stops at NULL characters. For binary data, fwrite() is appropriate.

Take Test
Q.30 Medium File Handling
A C program must securely close a file and handle potential errors. What is the best approach?
A if(fclose(fp) != 0) { handle_error(); }
B fclose(fp); assuming it always succeeds
C Just let the OS close file when program exits
D Use multiple fclose() calls for safety
Correct Answer:  A. if(fclose(fp) != 0) { handle_error(); }
EXPLANATION

fclose() returns 0 on success and EOF (-1) on error. Checking return value ensures buffered data is flushed and handles errors like write failures.

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