C Programming — File Handling
C language from basics to advanced placement prep
100 Questions 10 Topics Take Test
Advertisement
Showing 31–40 of 100 questions in File Handling
Q.31 Medium File Handling
In competitive exam file handling questions, if fseek() is called with SEEK_CUR, what does it indicate?
A Seek relative to current file pointer position
B Seek from the beginning of file
C Seek from the end of file
D Seek to current directory location
Correct Answer:  A. Seek relative to current file pointer position
EXPLANATION

SEEK_CUR is a constant meaning 'current position'. fseek(fp, offset, SEEK_CUR) moves the pointer by 'offset' bytes from its current position.

Take Test
Q.32 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
Q.33 Hard File Handling
When writing binary structures with fwrite(), which precaution is essential for portability across different systems?
A Account for struct padding and alignment differences
B Always use text mode instead of binary
C Ensure file size matches sizeof(struct) * count
D Use network byte order for all integers
Correct Answer:  A. Account for struct padding and alignment differences
EXPLANATION

Struct padding varies by architecture and compiler. Binary files written on one system may not read correctly on another due to different alignment. Use serialization techniques for portability.

Take Test
Q.34 Hard File Handling
How can you determine the size of a file in bytes using standard C functions?
A Use fseek(fp, 0, SEEK_END) followed by ftell(fp)
B Use stat() function on the filename
C Use fstat() on the file descriptor
D All of the above methods work
Correct Answer:  D. All of the above methods work
EXPLANATION

Multiple methods exist: fseek/ftell repositions and returns position, stat() gets file info including size, and fstat() does similar with file descriptor. All are valid.

Take Test
Q.35 Medium File Handling
A program reads structured data: fread(ptr, sizeof(struct), count, fp). If fread() returns a value less than 'count', what could be the reason?
A End of file reached before reading all items
B File pointer is NULL
C sizeof(struct) is too large
D The file was opened in text mode instead of binary
Correct Answer:  A. End of file reached before reading all items
EXPLANATION

fread() returns the number of items actually read. If less than 'count' is returned, it indicates EOF was reached or a read error occurred before completing the request.

Take Test
Q.36 Medium File Handling
In a file opened with 'rb+' mode, what operations can be performed?
A Reading and writing binary data without truncating
B Only reading binary data
C Only writing binary data
D Reading, writing, and truncating
Correct Answer:  A. Reading and writing binary data without truncating
EXPLANATION

'rb+' opens a binary file for both reading and writing. The file must exist. Existing content is preserved and can be overwritten at any position.

Take Test
Q.37 Medium File Handling
What is the difference between 'rb' and 'r' modes when opening a file?
A 'rb' opens in binary mode, 'r' opens in text mode with newline conversion
B 'rb' allows reading, 'r' allows both reading and writing
C Both are identical in modern C compilers
D 'r' is faster than 'rb' for large files
Correct Answer:  A. 'rb' opens in binary mode, 'r' opens in text mode with newline conversion
EXPLANATION

'rb' (binary read) reads data as-is without converting line endings. 'r' (text read) converts platform-specific newlines (\r\n on Windows) to \n.

Take Test
Q.38 Medium File Handling
A program needs to update specific bytes in the middle of a file without affecting other data. Which approach is most suitable?
A Use fseek() to position pointer, then fwrite() the new data
B Read entire file, modify in memory, write back
C Use file append mode to add changes
D Create new file with corrected data
Correct Answer:  A. Use fseek() to position pointer, then fwrite() the new data
EXPLANATION

Using fseek() with r+ or w+ mode allows direct access to specific file positions. This is efficient for small modifications without rewriting the entire file.

Take Test
Q.39 Medium File Handling
Which of the following correctly reads a string from file with size limit?
A fgets(buffer, size, fp)
B fscanf(fp, "%s", buffer)
C fgetc(fp)
D fread(buffer, 1, size, fp)
Correct Answer:  A. fgets(buffer, size, fp)
EXPLANATION

fgets(buffer, size, fp) safely reads up to (size-1) characters or until newline from file fp, preventing buffer overflow. fscanf() is vulnerable to overflow.

Take Test
Q.40 Medium File Handling
What does the function ftell(fp) return in file handling?
A Current position of file pointer from the beginning
B Total size of the file in bytes
C Number of bytes read so far
D End-of-file status
Correct Answer:  A. Current position of file pointer from the beginning
EXPLANATION

ftell(fp) returns the current position of the file pointer (in bytes) from the beginning of the file. Returns -1L on error.

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