ftell, fseek, rewind

Function Prototype

long ftell(FILE *fp);
int fseek(FILE *fp, long offset, int whence);
void rewind(FILE *fp);

Function

ftell

Return current file offset by bytes

fseek

Same as lseek

rewind

Same as fseek(fp, 0, SEEK_SET)

Remark

Binary Files

Under ANSI C, using SEEK_END with binary files has no guaranteed behavior. While this works reliably on Unix systems, other platforms may add padding bytes at the end of files, making the true end position ambiguous. For portable code, avoid SEEK_END with binary files.

Text Files

For text files, only use SEEK_SET with either zero or a value previously returned by ftell(). Different systems use different line endings (e.g., \r\n for Windows), and the C library performs automatic translation between them. This means byte offsets in the physical file don’t correspond to logical positions in your program’s view. Arbitrary seeks are therefore unsafe—only the file beginning and positions recorded by ftell() are guaranteed to work correctly across platforms.