Home Subjects C Programming File Handling

C Programming
File Handling

C language from basics to advanced placement prep

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 51–60 of 100
Topics in C Programming
Q.51 Hard File Handling
A program writes binary data using fwrite() but reads it back with fprintf(). What will happen?
A Data will be read correctly
B Binary data will be misinterpreted; text formatting won't apply
C Program will crash
D Only null bytes will be read
Correct Answer:  B. Binary data will be misinterpreted; text formatting won't apply
EXPLANATION

Mixing write (fwrite - binary) and read (fprintf - formatted text) functions on same data causes mismatch. Binary data won't have format specifiers; misinterpretation results.

Take Test
Q.52 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.

Take Test
Q.53 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.

Take Test
Q.54 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.

Take Test
Q.55 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).

Take Test
Q.56 Easy File Handling
What is returned by fopen() if file opening fails?
A Returns -1
B Returns NULL pointer
C Returns empty string
D Throws an exception
Correct Answer:  B. Returns NULL pointer
EXPLANATION

fopen() returns NULL pointer on failure. Proper practice is to check: if(fp == NULL) {...error handling...}. Returning -1 is for system calls like open().

Take Test
Q.57 Easy File Handling
Which function reads formatted data from a file?
A fgets()
B fscanf()
C fread()
D getc()
Correct Answer:  B. fscanf()
EXPLANATION

fscanf() reads formatted data from file (like scanf() but from file). fgets() reads strings. fread() reads binary data. getc() reads single character.

Take Test
Q.58 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.

Take Test
Q.59 Easy File Handling
What will fseek(fp, 0, SEEK_END) do?
A Moves pointer to beginning of file
B Moves pointer to end of file
C Moves pointer 0 bytes forward
D Closes the file
Correct Answer:  B. Moves pointer to end of file
EXPLANATION

SEEK_END moves pointer relative to end of file. Offset 0 means go to the exact end. SEEK_SET (beginning) and SEEK_CUR (current) are other origin options.

Take Test
Q.60 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.

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