Chapter Contents

Previous

Next
ftell

ftell



Obtain the Current File Position

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdio.h>

long int ftell(FILE *f);


DESCRIPTION

ftell returns the current position of the stream associated with the FILE object addressed by f , expressed as a long integer. The value returned by ftell can be passed later to a call to the fseek function for the same stream to restore the position at the time of the call to ftell .

When ftell is called for a binary stream, the position is expressed as the number of bytes from the start of the file. ftell is supported for a binary stream only when the "rel" access method is used or for an HFS file.

When ftell is called for a text stream, the value returned is an encoded form of the file position as stored by the fgetpos function. For a text stream, the difference between two file positions may not be related to the amount of separation between the two positions.

See File positioning with fseek and ftell for more information on the implementation of ftell . See OS/390 Files with Restricted Positioning and CMS Files with Restricted Positioning for file types for which ftell is not fully implemented.


RETURN VALUE

ftell returns the current file position expressed as a long int , if possible. If ftell is unable to successfully determine the file position, or if it cannot be stored in a long int , -1L is returned. In the latter case, an appropriate value is also stored in errno . See the list of errno values in The errno Variable.


PORTABILITY

Be cautious when porting an ftell application because the implementation of ftell varies from system to system and library to library.


IMPLEMENTATION

Refer to File positioning with fseek and ftell for implementation details.


EXAMPLE

This example counts the number of desired items in the file after the current position. Then it allocates storage, restores the file position, and reads the items:

#include <stdio.h>

main()
{
struct item {
   char name[40];
   int age;
   };

struct item new_item;
struct item *all_items;
long start;                       /* file position pointer      */

FILE *f;
int i, count=0;

f = fopen("tso:ITEMFILE", "rb");
start = ftell(f);                 /* Set file position pointer. */

   /* Count the number of items in the file with age over 20.   */
while(!feof(f) && !ferror(f)) {
   if (fread(&new_item, sizeof(new_item), 1, f))
      if (new_item.age > 20)
         ++count;
   if (ferror(f)) {
      puts("Error while reading file..exiting.");
      exit(1);
   }
}

   /* Seek to START location. */
if (fseek(f, start, SEEK_SET)) {
   puts("Could not locate start of file.");
   exit(1);
}

   /* Allocate space for all items. */
all_items = (struct item*)calloc(count, sizeof(struct item));
if (!all_items)                   /* Was memory allocated?      */
   exit(1);

   /* Read in items and store only the ones with age > 20.      */
i = 0;
while (!feof(f) && i < count)
   if (fread((all_items+i), sizeof(struct item), 1, f))
      if (all_items[i].age > 20)
         i++;
}


RELATED FUNCTIONS

fseek , fsetpos , lseek


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.