Govt Exams
The 'r+' mode opens a file for both reading and writing without truncating it. The file must exist.
ferror() returns a non-zero value if an error flag is set for the stream, otherwise returns 0.
fgets() reads a string from file including whitespace until newline or EOF is encountered or max length is reached.
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.
fgetc() is a function that reads a character from a file, while getc() is a macro. Both serve the same purpose but getc() may be faster due to macro expansion.
feof() returns true (non-zero) if the end-of-file indicator is set for the given file stream, otherwise it returns 0.