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(), andfsetpos()return0, andftell()returns the current offset. - Otherwise,
-1is returned anderrnois set to indicate the error. ๐ญ
๐ ERRORS ๐
EINVALโ Thewhenceargument tofseek()was notSEEK_SET,SEEK_END, orSEEK_CUR. Or: the resulting file offset would be negative. (negative offset?? not today bestie)ESPIPEโ The file descriptor underlyingstreamis 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 ๐
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
- Copyright 1990-1991, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms