Entrance Exams
Govt. Exams
The 'b' flag specifies binary mode, which prevents any translation of line endings or special characters.
The 'r' mode opens a file for reading only. The file must exist, otherwise fopen() returns NULL.
Opening a file in 'w' mode truncates the file to zero length if it exists, discarding all previous content.
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.
fread() syntax is fread(ptr, size, count, stream). To read 100 bytes, size=1 and count=100. Option B reads 100 items of 1 byte each, which is equivalent but less clear.
SEEK_END moves the file pointer to the end of the file. Offset can be 0 or negative to move before the end. SEEK_SET starts from beginning, SEEK_CUR from current position.
This code checks if fopen() returned NULL, which indicates the file failed to open. This is the standard error checking method for file operations.
rewind() moves the file pointer to the beginning of the file and clears the EOF indicator. It's equivalent to fseek(file, 0, SEEK_SET).
fwrite() returns the number of items successfully written to the file, which may be less than the count requested if an error occurs.
"r+" mode opens a file for both reading and writing without truncating it. The file must exist. "w+" creates a new file or truncates existing one.