C Reference ยท stdio.h
C fseek(), ftell() and rewind() functions
Reference for C fseek(), ftell(), rewind(), fgetpos() and fsetpos(): jump around in a file, find its size, read the last bytes and avoid text-mode pitfalls.
Every open file has a current position: the place where the next read or write happens. Reading and writing move it forward automatically. fseek() moves it somewhere else, ftell() reports where it is, and rewind() jumps back to the start.
Together they let you read a file in any order, patch a record in the middle of a file, and measure a file's size without reading it. They work best on binary streams and on files that are seekable at all: a terminal or a pipe cannot be repositioned.
#include <stdio.h> int fseek(FILE *stream, long offset, int origin); long ftell(FILE *stream); void rewind(FILE *stream);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int fseek(FILE *stream, long offset, int origin); 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);
| Name | Description |
|---|---|
offset |
Number of bytes to move, positive or negative depending on origin. |
origin |
SEEK_SET (from the start), SEEK_CUR (from the current position) or SEEK_END (from the end). |
pos |
An opaque position saved by fgetpos() and restored by fsetpos(). |
Returns: fseek() and fsetpos() return 0 on success and non-zero on failure. ftell() returns the position as a byte offset, or -1L on error (with errno set). rewind() returns nothing.
Errors / undefined behavior: a negative resulting position fails. fseek() on a stream that cannot seek (a pipe, a terminal) fails with ESPIPE.
Usage notes
- Binary files, bytes. With
"rb"and"wb"the offsets are plain byte counts. In text mode only offsets returned byftell()(and0) are guaranteed to work, because Windows line endings change the byte count.SEEK_ENDon a text stream is not reliable by the standard. - Clearing end-of-file. A successful
fseek()orrewind()clears the stream's end-of-file flag.rewind(f)isfseek(f, 0, SEEK_SET)plus clearing the error flag too. - Reading and writing on one stream. On a
+mode stream, put anfseek(),fsetpos(),rewind()orfflush()between a write and a following read (and the other way round). - Seeking past the end is allowed. A later write creates a hole filled with zero bytes on most systems; a read there returns
EOF. - The
longlimit.ftell()returns along, which is 32 bits on Windows, so it fails for files over 2 GB. On POSIX systems usefseeko()andftello()withoff_t, orfgetpos()andfsetpos()for opaque, size-independent positions. - File size trick.
fseek(f, 0, SEEK_END); ftell(f);is common, but for a file that may be changing, or on a stream you do not own,fstat()(POSIX) is cleaner. - Always check
fseek()when the offset comes from user data or from a file: a corrupt record number should not crash the program. - Append mode ignores the position on writes: the data always lands at the end of the file.
- Cost: a seek discards the stream's buffer, so many small reads in random order are slower than sequential reads.