A program processes a file with multiple reading and writing operations without closing/reopening. What is a potential issue with file buffering?
AData written to buffer may not be physically written to disk until fflush() or fclose()
BFile pointer position becomes unreliable
CRead operations always return cached data from memory
DFile 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.
When writing binary structures with fwrite(), which precaution is essential for portability across different systems?
AAccount for struct padding and alignment differences
BAlways use text mode instead of binary
CEnsure file size matches sizeof(struct) * count
DUse 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.
How can you determine the size of a file in bytes using standard C functions?
AUse fseek(fp, 0, SEEK_END) followed by ftell(fp)
BUse stat() function on the filename
CUse fstat() on the file descriptor
DAll 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.
A program reads structured data: fread(ptr, sizeof(struct), count, fp). If fread() returns a value less than 'count', what could be the reason?
AEnd of file reached before reading all items
BFile pointer is NULL
Csizeof(struct) is too large
DThe 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.
A program needs to update specific bytes in the middle of a file without affecting other data. Which approach is most suitable?
AUse fseek() to position pointer, then fwrite() the new data
BRead entire file, modify in memory, write back
CUse file append mode to add changes
DCreate 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.
Which of the following correctly reads a string from file with size limit?
Afgets(buffer, size, fp)
Bfscanf(fp, "%s", buffer)
Cfgetc(fp)
Dfread(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.