Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#include <stdio.h>
long int ftell(FILE *stream);
#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>
long int ftell_unlocked(FILE *stream);
The ftell() function obtains the current value of the file position indicator for the stream pointed to by stream.
Behavior for binary streams: ANSI states that the ftell() function returns relative byte offsets from the beginning of the file for binary files. Under z/OS® XL C/C++, this is true except for record-oriented files that have variable length records. For these types of files, the ftell() function returns an encoded offset.
If you want to use relative-byte offsets for these types of files, you can either open the file with the BYTESEEK fopen() function option or set the _EDC_BYTE_SEEK environment variable before opening.
Behavior for text streams: The ftell() function returns an encoded offset for text streams.
Behavior for record I/O: For files opened for record I/O using the type=record open mode parameter, the ftell() function returns the relative record offset of the current file position from the beginning of the file. All offset values are given in terms of records.
Behavior for blocked I/O: For files opened for blocked I/O using the type=blocked open mode parameter, the ftell() function returns the relative block offset of the current file position from the beginning of the file. All offset values are given in terms of blocks.
Multivolume data sets performance: Using the fgetpos() and fsetpos() functions generally results in better repositioning performance compared to the ftell() and fseek() functions when working with multivolume data sets.
Large file support for MVS data sets, VSAM data sets, and z/OS UNIX files: For AMODE 31 C/C++ applications, the ftell() function accepts a signed 4-byte offset and therefore cannot be used to directly or relatively position to offsets beyond 2 GB - 1. To avoid repositioning limitations, AMODE 31 C/C++ applications should define the _LARGE_FILES feature test macro before any headers are included and replace the ftell() function with the ftello() function. For AMODE 64 C/C++ applications, there are no restrictions on using the ftell() function with large files. The AMODE 64 version automatically accepts a signed 8-byte offset.
The ftell_unlocked() function is functionally equivalent to the ftell() function with the exception that it is not threadsafe. The ftell() function can safely be used in a multithreaded application if, and only if, it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.
If successful, the ftell() function returns the calculated value.
If unsuccessful, the ftell() function returns -1 and sets errno to a positive value.
/* This example opens the file myfile.dat for reading.
The current file pointer position is stored in the variable pos.
*/
#include <stdio.h>
int main(void)
{
FILE *stream
long int pos;
stream = fopen("myfile.dat", "rb");
/* The value returned by ftell can be used by fseek()
to set the file pointer if 'pos' is not -1 */
if ((pos = ftell(stream)) != EOF)
printf("Current position of file pointer found\n");
fclose(stream);
}