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 21–30 of 52
Topics in C Programming
Q.21 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.

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

Test
Q.23 Medium File Handling
Which mode allows both reading and appending without truncating existing content?
A a+
B w+
C r+
D ab+
Correct Answer:  A. a+
EXPLANATION

'a+' mode opens file for reading and appending. New data is added at the end without removing existing content. 'w+' would truncate the file.

Test
Q.24 Medium File Handling
In a file opened in "a+" mode, where does the file pointer initially point?
A Beginning of file
B End of file
C Middle of file
D Undefined location
Correct Answer:  B. End of file
EXPLANATION

In append mode "a" or "a+", file pointer initially points to end of file for writing. For reading in "a+", you must explicitly seek to read from start.

Test
Q.25 Medium File Handling
What does fflush(fp) do when fp is a file pointer?
A Clears the file content
B Writes buffered data to disk without closing file
C Moves file pointer to beginning
D Reads pending data from disk into buffer
Correct Answer:  B. Writes buffered data to disk without closing file
EXPLANATION

fflush(fp) forces buffered output to be written to disk immediately without closing the file. Useful for ensuring data safety before risky operations.

Test
Q.26 Medium File Handling
A program reads a CSV file with fgets() storing data in a 256-byte buffer. Some lines exceed 255 characters. How should this be handled?
A Use larger buffer size for each fgets() call
B Use fread() with binary mode instead
C Multiple fgets() calls are needed; accumulate partial reads
D Use fscanf() which handles large lines automatically
Correct Answer:  A. Use larger buffer size for each fgets() call
EXPLANATION

Best solution is allocate larger buffer. If line is 500 chars, use buffer >= 501. Option C partially works but complicates newline handling. fscanf() doesn't automatically handle long lines.

Test
Q.27 Medium File Handling
Which of the following modes will truncate an existing file?
A "r+"
B "a"
C "w"
D "a+"
Correct Answer:  C. "w"
EXPLANATION

"w" mode opens file for writing and truncates it to zero length if it exists. "r+" doesn't truncate. "a" and "a+" are append modes that don't truncate.

Test
Q.28 Medium File Handling
A binary file contains 500 structures of size 32 bytes each. To skip the first 10 structures and read the 11th one, which fseek() call is correct?
A fseek(fp, 10, SEEK_SET)
B fseek(fp, 320, SEEK_SET)
C fseek(fp, 11*32, SEEK_SET)
D fseek(fp, 9*32, SEEK_SET)
Correct Answer:  B. fseek(fp, 320, SEEK_SET)
EXPLANATION

To skip first 10 structures and read 11th: 10 * 32 = 320 bytes from start. Option B (320 bytes) positions pointer at start of 11th structure (320 bytes offset).

Test
Q.29 Medium File Handling
A program uses fprintf() to write 1000 integers to a file but doesn't call fclose(). What could happen?
A All data is automatically saved to disk
B Data in buffer may be lost if program terminates
C File becomes corrupted
D The program will hang indefinitely
Correct Answer:  B. Data in buffer may be lost if program terminates
EXPLANATION

Without fclose() or fflush(), buffered data remains in memory. If program terminates, that data never reaches disk. System may flush on exit but it's unreliable.

Test
Q.30 Medium File Handling
A file is opened in binary mode. How will fseek() behave differently compared to text mode?
A No difference in behavior
B In binary mode, fseek() always fails
C In text mode, fseek() may not work reliably due to character encoding differences
D In binary mode, negative offsets are not allowed
Correct Answer:  C. In text mode, fseek() may not work reliably due to character encoding differences
EXPLANATION

In text mode, fseek() behavior is implementation-dependent due to line-ending conversions (\r\n vs \n). Binary mode provides more predictable fseek() behavior with absolute byte positions.

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