ftell() — ftello() — Get Current Position

Format

#include <stdio.h>
long int ftell(FILE *stream);
off_t ftello(FILE *stream);

Language Level: ANSI

Threadsafe: Yes.

Integrated File System Interface: The ftello() function is not available when SYSIFCOPT(*NOIFSIO) is specified on the compilation command.

Description

The ftell() and ftello() functions find the current position of the file associated with stream. For a fixed-length binary file, the value that is returned is an offset relative to the beginning of the stream.

For file in the QSYS library system, the ftell() and ftello() functions return a relative value for fixed-format binary files and an encoded value for other file types. This encoded value must be used in calls to the fseek() and fseeko()functions to positions other than the beginning of the file.

Return Value

The ftell() and ftello() functions return the current file position. On error, ftell() and ftello() return –1, cast to long and off_t respectively, and set errno to a nonzero value.

The value of errno can be set to:

Value
Meaning
ENODEV
Operation was attempted on a wrong device.
ENOTOPEN
The file is not open.
ENUMMBRS
The file is open for multi-member processing.
ENUMRECS
Too many records.
ERECIO
The file is open for record I/O.
ESTDERR
stderr cannot be opened.
ESTDIN
stdin cannot be opened.
ESTDOUT
stdout cannot be opened.
EIOERROR
A non-recoverable I/O error occurred.
EIORECERR
A recoverable I/O error occurred.

The ftell() and ftello() functions are not supported for files that are opened with type=record.

Example that uses ftell()

This example opens the file mylib/myfile for reading. It reads enough characters to fill half of the buffer and prints out the position in the stream and the buffer.

#include  <stdio.h>
 
#define NUM_ALPHA  26
#define NUM_CHAR    6
 
int main(void)
{
  FILE * stream;
  int i;
  char ch;
 
  char buffer[NUM_ALPHA];
  long position;
 
  if (( stream = fopen("mylib/myfile", "r")) != NULL )
  {
    /* read into buffer */
    for ( i = 0; ( i  < NUM_ALPHA/2 ) && ((buffer[i] = fgetc(stream)) != EOF ); ++i )
        if (i==NUM_CHAR-1)  /* We want to be able to position the   */
                                     /* file pointer to the character in */
                                     /* position NUM_CHAR                */
           position = ftell(stream);
 
    buffer[i] = '\0';
  } printf("Current file position is %d\n", position); 
    printf("Buffer contains: %s\n", buffer);
}

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]