13.11. ftell()

Tells you where a particular file is about to read from or write to.


#include <stdio.h>

long ftell(FILE *stream);


This function is the opposite of fseek(). It tells you where in the file the next file operation will occur relative to the beginning of the file.

It's useful if you want to remember where you are in the file, fseek() somewhere else, and then come back later. You can take the return value from ftell() and feed it back into fseek() (with whence parameter set to SEEK_SET) when you want to return to your previous position.

Return Value

Returns the current offset in the file, or -1 on error.


long pos;

// store the current position in variable "pos":
pos = ftell(fp);

// seek ahead 10 bytes:
fseek(fp, 10, SEEK_CUR);

// do some mysterious writes to the file

// and return to the starting position, stored in "pos":
fseek(fp, pos, SEEK_SET);

See Also

fseek(), rewind(), fgetpos(), fsetpos()