Search code examples
cfileio

Why is the beginning of a C file stream called `SEEK_SET`?


There are three origin constants you can use in functions like fseek to determine from where your offset is counted: SEEK_SET, SEEK_CUR, and SEEK_END. SEEK_CUR and SEEK_END seem self-explanatory to mean the current position and end of the file stream, but why is SEEK_SET used to mean the beginning? Why not something like SEEK_BEG?


Solution

  • Because you can add an offset. By using SEEK_SET, you can explicitly set an offset. (By adding it to the beginning)

    From the manpage of fseek:

    The new position, measured in bytes, is
    obtained by adding offset bytes to the position specified by whence.
    If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is
    relative to the start of the file, the current position indicator, or
    end-of-file, respectively.
    

    From the manpage of lseek:

       SEEK_SET
              The file offset is set to offset bytes.
    
       SEEK_CUR
              The file offset is set to its current location plus offset
              bytes.
    
       SEEK_END
              The file offset is set to the size of the file plus offset
              bytes.