Entrance Exams
Govt. 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.
ferror() returns non-zero if an error has occurred in file operations. In critical applications like banking, this must be checked to ensure data integrity. This is different from EOF (checked by feof()).
fgets() with a fixed buffer reads one line at a time, maintaining constant memory usage regardless of file size. Option A wastes memory, option D is slowest, and option C may cause issues with large files on some systems.
fwrite(ptr, size, nmemb, fp) returns the number of complete items written, not bytes. If writing 10 items of 4 bytes each but only 35 bytes written, it returns 8, not 35.
fseek(fp, -10, SEEK_END) positions the pointer 10 bytes before the end of file. For a 100-byte file, this places it at position 90 (0-indexed). Negative offsets with SEEK_END are valid.
'r+' mode allows both reading and writing without truncating the file. 'w+' truncates the file on opening, 'a+' is for append operations. 'r+' preserves existing content while allowing modifications.
'w' opens file in text mode (converts line endings), while 'wb' opens in binary mode (no conversion). This matters when handling platform-specific line endings (\r\n vs \n).
feof() is the standard C library function that returns non-zero if EOF has been reached on the given file stream. Other options are not valid C functions.
Changed file format causes struct misalignment, leading to buffer overflow or garbage data. No automatic detection in C. Need version headers or checksums.