Entrance Exams
Govt. Exams
SEEK_CUR is a constant meaning 'current position'. fseek(fp, offset, SEEK_CUR) moves the pointer by 'offset' bytes from its current position.
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.
Struct padding varies by architecture and compiler. Binary files written on one system may not read correctly on another due to different alignment. Use serialization techniques for portability.
Multiple methods exist: fseek/ftell repositions and returns position, stat() gets file info including size, and fstat() does similar with file descriptor. All are valid.
fread() returns the number of items actually read. If less than 'count' is returned, it indicates EOF was reached or a read error occurred before completing the request.
'rb+' opens a binary file for both reading and writing. The file must exist. Existing content is preserved and can be overwritten at any position.
'rb' (binary read) reads data as-is without converting line endings. 'r' (text read) converts platform-specific newlines (\r\n on Windows) to \n.
Using fseek() with r+ or w+ mode allows direct access to specific file positions. This is efficient for small modifications without rewriting the entire file.
fgets(buffer, size, fp) safely reads up to (size-1) characters or until newline from file fp, preventing buffer overflow. fscanf() is vulnerable to overflow.
ftell(fp) returns the current position of the file pointer (in bytes) from the beginning of the file. Returns -1L on error.