Rules around a read-modify-write on an update stream

A program reads one 40-byte record from a binary file opened with mode "r+b", changes one field in memory, and then overwrites the same record in place.


To return to the start of the record it has just read, the program calls fseek with SEEK_CUR and an offset of bytes. That seek also separates the read from the following write, which an update stream requires. If the program instead writes first and then wants to read, it needs either a file-positioning call or a call to between the write and the read.


When it succeeds, fseek returns . On a binary stream, ftell reports the current position as a count of bytes from the start of the file, so with 40-byte records the record with zero-based index 7 starts at byte .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Block Files and Random Files Questions: