fseek(3)

๐Ÿ’– fseek(3) โ€” the "okay bestie let's rewind" page ๐Ÿ’–

Hiii so this is the manual page for fseek(3) (that's the man page name, the libc crew lives in section 3, iykyk โœจ). It's about literally moving the little cursor thingy inside a stream. Five functions, one vibe: getting your file position exactly where you want it. ๐ŸŒธ


โœจ NAME โœจ

fgetpos, fseek, fsetpos, ftell, rewind โ€” reposition a stream ๐ŸŽ€

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc)

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <stdio.h>

int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);

void rewind(FILE *stream);

int fgetpos(FILE *restrict stream, fpos_t *restrict pos);
int fsetpos(FILE *stream, const fpos_t *pos);

๐Ÿ’… DESCRIPTION ๐Ÿ’…

Okay so fseek() sets the file position indicator for the stream pointed to by stream. The new position, measured in bytes, is obtained by adding offset bytes to the position specified by whence. And when 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. Slay. ๐ŸŽฏ

When an fseek() call succeeds it also clears the end-of-file indicator for the stream and undoes any effects of the ungetc(3) function on that same stream. like it never happened bestie ๐Ÿ’€

The ftell() function obtains the current value of the file position indicator for the stream pointed to by stream. It's literally just "where am I rn". ๐Ÿ“

The rewind() function sets the file position indicator for the stream pointed to by stream to the beginning of the file. It is equivalent to:

(void) fseek(stream, 0L, SEEK_SET)

except that the error indicator for the stream is also cleared (see clearerr(3)). so rewind() is the "forget everything and start over" queen ๐Ÿ‘‘

The fgetpos() and fsetpos() functions are alternate interfaces equivalent to ftell() and fseek() (with whence set to SEEK_SET), setting and storing the current value of the file offset into or from the object referenced by pos. On some non-UNIX systems, an fpos_t object may be a complex object, and these routines may be the only way to portably reposition a text stream. so like, cross-platform icon behavior ๐Ÿงš

โš ๏ธ heads up: if the stream refers to a regular file and the resulting stream offset is beyond the size of the file, subsequent writes will extend the file with a hole, up to the offset, before committing any data. See lseek(2) for details on file seeking semantics. spicy but true ๐ŸŒถ๏ธ

๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

  • rewind() returns no value. it's a void girlie, she gives nothing back ๐Ÿ’…
  • Upon successful completion, fgetpos(), fseek(), and fsetpos() return 0, and ftell() returns the current offset.
  • Otherwise, -1 is returned and errno is set to indicate the error. ๐Ÿ˜ญ

๐Ÿ’€ ERRORS ๐Ÿ’€

  • EINVAL โ€” The whence argument to fseek() was not SEEK_SET, SEEK_END, or SEEK_CUR. Or: the resulting file offset would be negative. (negative offset?? not today bestie)
  • ESPIPE โ€” The file descriptor underlying stream is not seekable (e.g., it refers to a pipe, FIFO, or socket). you genuinely cannot rewind a pipe, that's not a skill issue, it's a physics issue ๐Ÿšซ

The functions fgetpos(), fseek(), fsetpos(), and ftell() may also fail and set errno for any of the errors specified for the routines fflush(3), fstat(2), lseek(2), and malloc(3). so many ways to get humbled fr fr ๐Ÿ™ˆ

๐ŸŒˆ ATTRIBUTES ๐ŸŒˆ

For an explanation of the terms used in this section, see attributes(7).

  • Interface: fseek(), ftell(), rewind(), fgetpos(), fsetpos()
    • Attribute: Thread safety
    • Value: MT-Safe

(yes all five of them, one big happy thread-safe family ๐Ÿ‘‘โœจ)

โญ STANDARDS โญ

C11, POSIX.1-2008.

๐ŸŽ€ HISTORY ๐ŸŽ€

POSIX.1-2001, C89. vintage but still serving ๐Ÿซถ

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

lseek(2), fseeko(3)


and that's the whole page bestie, go move some file pointers, you got this ๐Ÿ’œ

this fine piece of documentation came to you from Linux man-pages 6.20, dated 2025-05-17, and it's still fseek(3) at heart. mwah ๐Ÿ’‹
๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms