FAT | RFS | XFS | ZFS

fseek()

PROTOTYPE

#include <stdio.h>

int fseek(FILE *file, long offset, int whence);

DESCRIPTION

fseek() sets the current position of the open regular file indicated by file. The new position is offset number of bytes from the position determined by whence, which is one of the following:

  • SEEK_SET - the new offset is offset bytes from the beginning of the file
  • SEEK_CUR - the new offset is its current value plus offset bytes
  • SEEK_END - the new offset is set to the file size plus offset bytes

offset is signed and may specify a backward or a forward seek, but it is illegal to seek back before a file’s beginning. fseek() may move the position forward past the end of existing data. If a write is performed while the file’s position is past its end, the gap between the current position and the previous file end is filled with zeros.

If successful, fseek() clears the end-of-file indicator, drops any byte stored on the file’s control block by ungetc(), and returns 0. Otherwise, it sets errno and returns -1.

ERROR CODES

EBADF file is not the handle of an open file.
EINVAL whence is invalid or the new position would be before the file’s start.
EISDIR The specified file is a directory.

EXAMPLE

/*-------------------------------------------------------------------*/
/* Output the first 12 characters of file in reverse order.          */
/*-------------------------------------------------------------------*/
for (i = 12; i >= 0; --i)
{
  if (fseek(file, i, SEEK_SET))
    return -1;
  putchar(getc(file));
}