Name
ftell - Finds the current position of a file pointer.
Syntax
#include <stdio.h>
long ftell(stream)
FILE *stream;
Description
The ftell function gets the current position of the file
pointer (if any) associated with stream. The position is
expressed as an offset relative to the beginning of the
stream.
Note that when a file is opened for appending data, the
current file position is determined by the last I/O
operation, not where the next write would occur. For
example, if a file is opened for an append and the last
operation was a read, the file position is the point where
the next read operation would start, not where the next
write would start. If no I/O operation has yet occurred on a
file opened for appending, then the file position is the
beginning of the file.
Return Value
The ftell function returns the current position. On error,
the function returns -1L and errno is set to one of the
following constants, defined in errno.h:
Constant Description
EBADF Bad file number. The stream argument
is not a valid file-handle value or
does not refer to an open file.
EINVAL Invalid argument. An invalid stream
argument was passed to the function.
On devices incapable of seeking (such as terminals and
printers), or when stream does not refer to an open file,
the return value is undefined.
See Also
fgetpos(DOS), fseek(DOS), lseek(DOS), tell(DOS)
Notes
The value returned by ftell may not reflect the physical
byte offset for streams opened in text mode, since text mode
causes carriage-return-line-feed (CR-LF) translation. Use
ftell in conjunction with the fseek function to remember and
return to file locations correctly.
Example
#include <stdio.h>
FILE *stream; long position; char list[100];
main()
{
stream = fopen("data","rb");
/* Move the pointer by reading data: */
fread(list,sizeof(char),100, stream);
/* Get position after read: */
position = ftell(stream);
printf("position = %ld\n", position);
}
This program opens a file named data for reading and tries
to read 100 characters. It then uses ftell to determine the
position of the file pointer and displays this position.
(printed 6/18/89)