ftell(), ftello(), ftello64()

QNX SDP8.0C Library ReferenceAPIDeveloper

Return the current position of a stream

Synopsis:

#include <stdio.h>

long int ftell( FILE* fp );

off_t ftello( FILE* fp );

off64_t ftello64( FILE* fp );

Arguments:

fp
The stream that you want to get the current position of.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The ftell(), ftello(), and ftello64() functions return the current position of the stream specified by fp. This position defines the character that will be read or written by the next I/O operation on the file.

The difference between these functions is the data type of the returned position. On 64-bit architectures, all these functions represent file offsets with a 64-bit integer type, so code will work equally well regardless of which is used. However, the ftello() function is preferred over ftell() because off_t is the correct type for representing a file offset in a POSIX-compatible OS; long is not.

Note:
The ftello64() function is a large-file support version of ftello() provided for backwards compatibility. If you're using large-file support functions and data types, you should define _LARGEFILE64_SOURCE with a value of 1 to ensure they appear in the name space. For more information, see Classification in What's in a Function Description?.

You can use the value returned by an ftell*() function in a subsequent call to the corresponding fseek*() function to restore the file position to a previous value.

Returns:

The current position of the file, or -1L if an error occurred (errno is set).

Examples:

#include <stdio.h>
#include <stdlib.h>

long filesize( FILE *fp )
{
    long int save_pos;
    long size_of_file;

    /* Save the current position. */
    save_pos = ftell( fp );
    
    /* Jump to the end of the file. */
    fseek( fp, 0L, SEEK_END );
    
    /* Get the end position. */
    size_of_file = ftell( fp );
    
    /* Jump back to the original position. */
    fseek( fp, save_pos, SEEK_SET );

    return( size_of_file );
}

int main( void )
{
    FILE *fp;

    fp = fopen( "file", "r" );

    if( fp != NULL ) {
        printf( "File size=%ld\n", filesize( fp ) );
        fclose( fp );
        
        return EXIT_SUCCESS;
    }
    
    return EXIT_FAILURE;
}

Classification:

ftell() is ANSI, POSIX 1003.1; ftello() is POSIX 1003.1; ftello64() is Large-file support

Safety:
Cancellation pointYes
Signal handlerNo
ThreadYes
Page updated: