ftell#

Purpose#

Gets the position of the file pointer in a file.

Format#

pos = ftell(fh)#
Parameters:

fh (scalar) – file handle of a file opened with fopen().

Returns:

pos (scalar) – current position of the file pointer in a file.

Remarks#

The ftell() function returns the position of the file pointer in terms of bytes from the beginning of the file. The call may fail if the file buffer needs to be flushed (see fflush()).

If an error occurs, ftell() returns -1. You can call fstrerror() to find out what the error was.

If you pass ftell() the handle of a file opened with open (i.e., a data set or matrix file), your program will terminate with a fatal error.

Example#

// Open a file and track the file pointer position
fname = tempname("/tmp", "ex", ".txt");
fh = fopen(fname, "wb");
call fputs(fh, "ABCDEFGHIJ");
call close(fh);

fh = fopen(fname, "rb");
pos = ftell(fh);
print "Initial position:" pos;

call fseek(fh, 5, 0);
pos = ftell(fh);
print "After seeking to byte 5:" pos;
call close(fh);

See also

Functions fopen(), fseek()