ftell
long  ftell ( FILE * stream );
stdio.h
  cplusplus.com  

Return the current position in a stream.
  Returns the current position pointed by the position indicator of the stream. When a file has been opened in binary mode the value obtained corresponds to the number of bytes from the beginning of the file. In files opened in text-mode this is not granted because of carriage-return translations under that mode. The value returned is valid for further calls to fseek.

Parameters.

stream
Pointer to an open file.

Return Value.
  On success, the current file pointer position is returned.
  If an error occurs -1L is returned and the global variable errno is set to a positive value.
  If the stream is unable to perform seeking operations (like modems) the return value is undefined.

Portability.
  Defined in ANSI-C.

Example.

/* ftell example : getting size of a file */
#include <stdio.h>

main ()
{
  FILE * pFile;
  long size;

  pFile = fopen ("myfile.txt","rb");
  if (pFile==NULL) perror ("Error opening file");
  else
  {
    fseek (pFile, 0, SEEK_END);
    size=ftell (pFile);
    fclose (pFile);
    printf ("Size of myfile.txt: %ld bytes.\n",size);
  }
  return 0;
}
This program opens example.txt for reading and calculates its size.
Output:
 Size of example.txt: 735 bytes

See also.
  fopen, fseek, fgetpos


© The C++ Resources Network, 2000