Home Subjects C Programming File Handling

C Programming
File Handling

C language from basics to advanced placement prep

21 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 21
Topics in C Programming
Q.11 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.

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

Test
Q.13 Hard File Handling
A large binary file of 10GB is being processed. Reading entire file into memory is impossible. Which approach is best?
A Use fread() to read file in chunks, process each chunk
B Use multiple fopen() calls on same file
C Split file manually into smaller files
D Use fscanf() which has built-in chunking
Correct Answer:  A. Use fread() to read file in chunks, process each chunk
EXPLANATION

fread(buffer, size, count, fp) efficiently reads file in manageable chunks. Process each chunk, then read next. This is standard practice for large file handling.

Test
Q.14 Hard File Handling
A program uses fgetc() to read 100,000 characters from a file sequentially. Which alternative would be more efficient?
A Use fgets() with larger buffer
B Use fread() with appropriate buffer size
C Use fscanf() with %c format
D Continue with fgetc(); efficiency doesn't matter
Correct Answer:  B. Use fread() with appropriate buffer size
EXPLANATION

fread() with buffer reads multiple bytes per call, reducing function call overhead. fgetc() makes 100,000 separate calls. fgets() is limited to reading up to newline.

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

Test
Q.16 Hard File Handling
What is the purpose of using fflush() in file handling, and when is it critical to use it?
A To move file pointer to the beginning of the file
B To write buffered data to the actual file on disk
C To close the file gracefully
D To clear error flags from the stream
Correct Answer:  B. To write buffered data to the actual file on disk
EXPLANATION

fflush() writes any buffered data to the underlying file. It's critical when you need to ensure data is written to disk before continuing, especially before reading the same file.

Test
Q.17 Hard File Handling
A program writes data using fprintf() and later attempts to read it back. However, the read operation fails intermittently. What could be the most likely cause?
A fprintf() is not suitable for output operations
B The buffer was not flushed before reading, causing incomplete data
C The file was opened in binary mode instead of text mode
D The file pointer was not reset after writing
Correct Answer:  B. The buffer was not flushed before reading, causing incomplete data
EXPLANATION

fprintf() buffers output. Without flushing (fflush() or fclose()), subsequent read operations may see incomplete data. This is a common source of bugs.

Test
Q.18 Hard File Handling
Consider using fseek() on a file opened in text mode with SEEK_END and a non-zero offset. What is the standard behavior?
A It is guaranteed to work correctly on all platforms
B It is undefined behavior and may not work correctly on all platforms
C It always returns an error
D It works only on Windows systems
Correct Answer:  B. It is undefined behavior and may not work correctly on all platforms
EXPLANATION

In text mode, fseek() with non-zero offset relative to SEEK_END is undefined behavior. Use fseek(fp, 0, SEEK_END) for reliable behavior.

Test
Q.19 Hard File Handling
In a program reading a binary file of 1000 integers, fread() is called as: fread(arr, sizeof(int), 1000, fp). If only 500 integers are present, what will fread() return?
A 500
B An error code
C NULL
D The bytes read (typically 2000)
Correct Answer:  A. 500
EXPLANATION

fread() returns the number of complete items successfully read, not the number of bytes. It will return 500 even if 1000 were requested.

Test
Q.20 Hard File Handling
Which of the following correctly implements appending to a file?
A FILE *fp = fopen("file.txt", "a");
B FILE *fp = fopen("file.txt", "w");
C FILE *fp = fopen("file.txt", "r+");
D FILE *fp = fopen("file.txt", "ab+");
Correct Answer:  A. FILE *fp = fopen("file.txt", "a");
EXPLANATION

Mode "a" opens a file for appending. New data is written at the end of the file. Mode "w" truncates the file, "r+" requires file to exist, "ab+" is appending in binary with both read/write.

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