[Previous]
[Contents]
[Next]

ftell()

return the current read/write position of a file

Synopsis:

#include <stdio.h>
long int ftell( FILE *fp );

Description:

The ftell() function returns the current read/write position of the file specified by fp. This position defines the character that will be read or written by the next I/O operation on the file. The value returned by ftell() can be used in a subsequent call to fseek() to set the file to the same position.

Returns:

The current read/write position of the file specified by fp. When an error is detected, -1L is returned. When an error has occurred, errno contains a value that indicates the type of error that has been detected.

Examples:

#include <stdio.h>

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

    save_pos = ftell( fp );
    fseek( fp, 0L, SEEK_END );
    size_of_file = ftell( fp );
    fseek( fp, save_pos, SEEK_SET );
    return( size_of_file );
  }

void main()
  {
    FILE *fp;

    fp = fopen( "file", "r" );
    if( fp != NULL ) {
      printf( "File size=%ld\n", filesize( fp ) );
      fclose( fp );
    }
  }

Classification:

ANSI

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

errno, fgetpos(), fopen(), fsetpos(), fseek()


[Previous]
[Contents]
[Next]