C Programming — File Handling
C language from basics to advanced placement prep
21 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 21 questions in File Handling
Q.1 Hard File Handling
In a program that processes binary files with variable-length records, what could cause fread() to return fewer items than requested without reaching EOF?
A A read error occurred during I/O operation
B The file is opened in text mode instead of binary mode
C The buffer size is larger than remaining file size
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

fread() can return fewer items due to: (A) I/O errors, (B) text mode conversions affecting byte count, or (C) insufficient data remaining. For robust code, check ferror() and feof() after fread() returns less than requested.

Take Test
Q.2 Hard File Handling
For implementing a file locking mechanism in a competitive exam environment, which approach correctly handles concurrent file access in POSIX systems?
A Using flock() or fcntl() system calls with F_WRLCK
B Opening file in 'a' mode ensures automatic locking
C Using fseek() to check if file is accessible
D Creating a separate lock file and checking its existence
Correct Answer:  A. Using flock() or fcntl() system calls with F_WRLCK
EXPLANATION

flock() and fcntl() with F_WRLCK provide kernel-level file locking in POSIX systems. Option B is false, option C doesn't provide locking, and option D is unreliable due to race conditions.

Take Test
Q.3 Hard File Handling
A program reads serialized objects using fread(). What's the primary risk when file format changes between versions?
A fread() will fail silently
B Buffer overflow or misaligned struct interpretation
C File size becomes invalid
D Automatic version detection prevents issues
Correct Answer:  B. Buffer overflow or misaligned struct interpretation
EXPLANATION

Changed file format causes struct misalignment, leading to buffer overflow or garbage data. No automatic detection in C. Need version headers or checksums.

Take Test
Q.4 Hard File Handling
When implementing a log file writer with concurrent access, what's the critical issue with standard fopen()?
A fopen() is too slow
B No built-in file locking mechanism
C fopen() doesn't support append atomically
D Memory leaks after multiple opens
Correct Answer:  B. No built-in file locking mechanism
EXPLANATION

Standard C fopen/fwrite lack file locking. Multiple processes can corrupt data. Need OS-specific locking (flock/fcntl on Unix) or use 'a' mode with small writes.

Take Test
Q.5 Hard File Handling
For competitive exam file validation: A 2GB file must be processed without loading entirely in memory. Which strategy is optimal?
A Read entire file into heap-allocated buffer
B Use mmap() for memory-mapped I/O
C Read in fixed-size chunks in loop
D Use external sorting tools
Correct Answer:  C. Read in fixed-size chunks in loop
EXPLANATION

Fixed-size chunk processing is standard for large files. mmap() helps but may not be portable. Loading entire file exhausts memory. External tools bypass C programming.

Take Test
Advertisement
Q.6 Hard File Handling
A program processes encrypted binary data with fread(). Why might using 'r' mode instead of 'rb' cause corruption?
A Text mode converts line endings, corrupting binary data
B Text mode stops at null bytes
C Text mode is slower
D Text mode reduces buffer size
Correct Answer:  A. Text mode converts line endings, corrupting binary data
EXPLANATION

Text mode on Windows converts CRLF to LF and vice versa, corrupting binary. Binary mode preserves exact bytes. Null byte handling is secondary concern.

Take Test
Q.7 Hard File Handling
What does rewind(fp) accomplish compared to fseek(fp, 0, SEEK_SET)?
A rewind() is faster
B rewind() also clears error indicators; fseek() doesn't
C They are functionally identical
D fseek() works only in binary mode
Correct Answer:  B. rewind() also clears error indicators; fseek() doesn't
EXPLANATION

rewind(fp) resets pointer AND clears EOF/error flags. fseek(fp, 0, SEEK_SET) only repositions. rewind is essentially: fseek(...); clearerr(...);

Take Test
Q.8 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.9 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.10 Hard File Handling
A program processes a file with multiple reading and writing operations without closing/reopening. What is a potential issue with file buffering?
A Data written to buffer may not be physically written to disk until fflush() or fclose()
B File pointer position becomes unreliable
C Read operations always return cached data from memory
D File automatically truncates after buffer fills
Correct Answer:  A. Data written to buffer may not be physically written to disk until fflush() or fclose()
EXPLANATION

File I/O uses buffering for efficiency. Data in write buffer stays in memory until fflush(fp) explicitly flushes or fclose() is called, risking data loss if program crashes.

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