When implementing a log file writer with concurrent access, what's the critical issue with standard fopen()?
Afopen() is too slow
BNo built-in file locking mechanism
Cfopen() doesn't support append atomically
DMemory leaks after multiple opens
Correct Answer:
B. No built-in file locking mechanism
EXPLANATION
Standard C fopen/fwrite lack file locking. Multiple processes can corrupt data. Need OS-specific locking (flock/fcntl on Unix) or use 'a' mode with small writes.
For competitive exam file validation: A 2GB file must be processed without loading entirely in memory. Which strategy is optimal?
ARead entire file into heap-allocated buffer
BUse mmap() for memory-mapped I/O
CRead in fixed-size chunks in loop
DUse external sorting tools
Correct Answer:
C. Read in fixed-size chunks in loop
EXPLANATION
Fixed-size chunk processing is standard for large files. mmap() helps but may not be portable. Loading entire file exhausts memory. External tools bypass C programming.
A program processes encrypted binary data with fread(). Why might using 'r' mode instead of 'rb' cause corruption?
AText mode converts line endings, corrupting binary data
BText mode stops at null bytes
CText mode is slower
DText mode reduces buffer size
Correct Answer:
A. Text mode converts line endings, corrupting binary data
EXPLANATION
Text mode on Windows converts CRLF to LF and vice versa, corrupting binary. Binary mode preserves exact bytes. Null byte handling is secondary concern.
A program crashes after reading a file. What's most likely the cause if error checking is minimal?
AFile doesn't exist but fopen() wasn't checked
BBuffer overflow during fread()
CInsufficient disk space
DFile permissions are read-only
Correct Answer:
A. File doesn't exist but fopen() wasn't checked
EXPLANATION
Not checking fopen() return value leads to NULL pointer dereference. Buffer overflow and permissions cause different errors. Disk space affects writing.
In a program processing a CSV file, what is the most robust approach for reading variable-length lines?
AUse fixed-size buffer with fgets()
BUse getc() in loop and dynamically allocate memory
CUse fscanf() with %s format
DRead entire file into memory at once
Correct Answer:
B. Use getc() in loop and dynamically allocate memory
EXPLANATION
Dynamic allocation with getc() handles variable-length lines robustly. fgets() with fixed buffer may truncate. fscanf() with %s doesn't handle spaces. Option D wastes memory.