Home Subjects C Programming File Handling

C Programming
File Handling

C language from basics to advanced placement prep

52 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 52
Topics in C Programming
Q.11 Medium File Handling
Which statement correctly counts non-comment lines in a text file?
A Read each line and skip if first non-whitespace char is '//'
B Use grep to filter comments before processing
C Read line with fgets(), check for '//' pattern, increment counter if not found
D Use fscanf() with format specifier to skip comment lines
Correct Answer:  C. Read line with fgets(), check for '//' pattern, increment counter if not found
EXPLANATION

Pure C solution uses fgets() to read lines and strstr() or manual checking for comment patterns. Option B uses external tool. Option D is inefficient.

Test
Q.12 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.

Test
Q.13 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.

Test
Q.14 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.

Test
Q.15 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.

Test
Q.16 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.

Test
Q.17 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.

Test
Q.18 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.

Test
Q.19 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.

Test
Q.20 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.

Test
IGET
IGET AI
Online · Exam prep assistant
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