FSEEK(3POSIX)
π FSEEK β the 3POSIX glow-up π
okay bestie, so this page lives in the POSIX Programmer's Manual, it's filed under 3POSIX, and it's literally the blueprint for moving your little read/write cursor around inside a stream. no cap. π¦
β¨ PROLOG β¨
so like, heads up: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so pls go consult the corresponding Linux manual page for the deets on Linux behavior), or β and this is the sad part π β the interface might not even be implemented on Linux at all.
π NAME π
fseek, fseeko β reposition a file-position indicator in a stream
it's giving "move the cursor, queen." π
πΈ SYNOPSIS πΈ
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);
int fseeko(FILE *stream, off_t offset, int whence);
π DESCRIPTION π
okay so the functionality on this reference page is aligned with the ISO C standard. if there's any conflict between what's written here and the ISO C standard, that conflict is unintentional β nobody did it on purpose, fr fr. this volume of POSIX.1β2017 defers to the ISO C standard. periodt.
fseek() shall set the file-position indicator for the stream pointed to by stream. and if a read or write error happens? the error indicator for the stream shall be set, and fseek() fails. π
the new position β measured in bytes from the beginning of the file β shall be obtained by adding offset to the position specified by whence. and what's that specified point? it's:
- the beginning of the file for
SEEK_SET - the current value of the file-position indicator for
SEEK_CUR - end-of-file for
SEEK_END
if the stream is gonna be used with wide-character input/output functions, the application shall ensure that offset is either 0 or a value returned by an earlier call to ftell() on the same stream β and that whence is SEEK_SET. that's the rule, no wiggle room. π«Ά
a successful call to fseek() shall clear the end-of-file indicator for the stream and undo any effects of ungetc() and ungetwc() on that same stream. and after an fseek() call, the next operation on an update stream may be either input or output. freedom β¨
if the most recent operation on a given stream β other than ftell() β is fflush(), then the file offset in the underlying open file description shall be adjusted to reflect the location specified by fseek().
fseek() shall allow the file-position indicator to be set beyond the end of existing data in the file. and if data is later written at this point, subsequent reads of data in that gap shall return bytes with the value 0 until data is actually written into the gap. spooky little zero-filled hole, iconic honestly. π³οΈ
the behavior of fseek() on devices which are incapable of seeking is implementation-defined. and the value of the file offset associated with such a device is undefined. so like⦠don't count on it, bestie.
if the stream is writable and buffered data had not been written to the underlying file, fseek() shall cause the unwritten data to be written to the file and shall mark the last data modification and last file status change timestamps of the file for update. flushing on the way out, we love a tidy queen. π
in a locale with state-dependent encoding, whether fseek() restores the stream's shift state is implementation-defined.
and finally: fseeko() shall be equivalent to fseek() except that the offset argument is of type off_t. that's the whole difference. that's it. the entire vibe shift. π
π₯ RETURN VALUE π₯
fseek() and fseeko() shall return 0 if they succeed. slay.
otherwise, they shall return -1 and set errno to indicate the error. not slay. π
π ERRORS π
fseek() and fseeko() shall fail if β either the stream is unbuffered or the stream's buffer needed to be flushed β and the call to fseek() or fseeko() causes an underlying lseek() or write() to be invoked, and:
EAGAINβ theO_NONBLOCKflag is set for the file descriptor and the thread would be delayed in the write operation.EBADFβ the file descriptor underlying the stream file is not open for writing, or the stream's buffer needed to be flushed and the file is not open.EFBIGβ an attempt was made to write a file that exceeds the maximum file size.EFBIGβ an attempt was made to write a file that exceeds the file size limit of the process.EFBIGβ the file is a regular file and an attempt was made to write at or beyond the offset maximum associated with the corresponding stream.EINTRβ the write operation was terminated due to the receipt of a signal, and no data was transferred.EINVALβ thewhenceargument is invalid. the resulting file-position indicator would be set to a negative value.EIOβ a physical I/O error has occurred, or the process is a member of a background process group attempting to perform awrite()to its controlling terminal,TOSTOPis set, the calling thread is not blockingSIGTTOU, the process is not ignoringSIGTTOU, and the process group of the process is orphaned. this error may also be returned under implementation-defined conditions.ENOSPCβ there was no free space remaining on the device containing the file.EOVERFLOWβ forfseek(), the resulting file offset would be a value which cannot be represented correctly in an object of typelong.EOVERFLOWβ forfseeko(), the resulting file offset would be a value which cannot be represented correctly in an object of typeoff_t.EPIPEβ an attempt was made to write to a pipe or FIFO that is not open for reading by any process; aSIGPIPEsignal shall also be sent to the thread.ESPIPEβ the file descriptor underlyingstreamis associated with a pipe, FIFO, or socket.
fseek() and fseeko() may fail if:
ENXIOβ a request was made of a nonexistent device, or the request was outside the capabilities of the device.
bestie note: the following sections are informative. i.e. they're vibes, not rules. π«§
π§ EXAMPLES π§
None.
(i know. i know. we were all rooting for a cute little code snippet and we got nothing. it's giving "left on read". π)
π APPLICATION USAGE π
None.
π RATIONALE π
None.
β FUTURE DIRECTIONS β
None.
πΈ SEE ALSO πΈ
- Section 2.5, Standard I/O Streams
fopen()fsetpos()ftell()getrlimit()lseek()rewind()ulimit()ungetc()write()
and from the Base Definitions volume of POSIX.1β2017: <stdio.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.
in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. (the original ate, this one is just the re-upload. π )
the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page bestie!! brought to you by IEEE/The Open Group, 2017, straight out of the POSIX Programmer's Manual β FSEEK(3POSIX), 2017 edition. mwah. πβ¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms