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 β€” the O_NONBLOCK flag 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 β€” the whence argument 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 a write() to its controlling terminal, TOSTOP is set, the calling thread is not blocking SIGTTOU, the process is not ignoring SIGTTOU, 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 β€” for fseek(), the resulting file offset would be a value which cannot be represented correctly in an object of type long.
  • EOVERFLOW β€” for fseeko(), the resulting file offset would be a value which cannot be represented correctly in an object of type off_t.
  • EPIPE β€” an attempt was made to write to a pipe or FIFO that is not open for reading by any process; a SIGPIPE signal shall also be sent to the thread.
  • ESPIPE β€” the file descriptor underlying stream is 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>


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. πŸ’‹βœ¨