Govt. Exams
Entrance Exams
fread() can return fewer items due to: (A) I/O errors, (B) text mode conversions affecting byte count, or (C) insufficient data remaining. For robust code, check ferror() and feof() after fread() returns less than requested.
flock() and fcntl() with F_WRLCK provide kernel-level file locking in POSIX systems. Option B is false, option C doesn't provide locking, and option D is unreliable due to race conditions.
Changed file format causes struct misalignment, leading to buffer overflow or garbage data. No automatic detection in C. Need version headers or checksums.
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.
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.
Text mode on Windows converts CRLF to LF and vice versa, corrupting binary. Binary mode preserves exact bytes. Null byte handling is secondary concern.
rewind(fp) resets pointer AND clears EOF/error flags. fseek(fp, 0, SEEK_SET) only repositions. rewind is essentially: fseek(...); clearerr(...);
fgets() is safest for line reading with size protection. For lines exceeding buffer size, use dynamic allocation or getline(). fscanf() with %s is vulnerable to buffer overflow.
fprintf() is text-oriented and interprets format specifiers, potentially modifying data. It also stops at NULL characters. For binary data, fwrite() is appropriate.
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.