cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : cstdio (stdio.h) : ftell
 
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstdio (stdio.h)
functions:
· clearerr
· fclose
· feof
· ferror
· fflush
· fgetc
· fgetpos
· fgets
· fopen
· fprintf
· fputc
· fputs
· fread
· freopen
· fscanf
· fseek
· fsetpos
· ftell
· fwrite
· getc
· getchar
· gets
· perror
· printf
· putc
· putchar
· puts
· remove
· rename
· rewind
· scanf
· setbuf
· setvbuf
· sprintf
· sscanf
· tmpfile
· tmpnam
· ungetc
· vfprintf
· vprintf
· vsprintf
macro constants:
· EOF
· FILENAME_MAX
· NULL
· TMP_MAX
objects:
· stderr
· stdin
· stdout
types:
· FILE
· fpos_t
· size_t

-

ftell function
long int ftell ( FILE * stream );
<cstdio>

Get current position in stream

Returns the current value of the position indicator of the stream.
For binary streams, the value returned corresponds to the number of bytes from the beginning of the file.
For text streams, the value is not guaranteed to be the exact number of bytes from the beginning of the file, but the value returned can still be used to restore the position indicator to this position using fseek.

Parameters

stream
Pointer to a FILE object that identifies the stream.

Return Value

On success, the current value of the position indicator is returned.
If an error occurs, -1L is returned, and the global variable errno is set to a positive value. This value can be interpreted by perror.

Example

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

int 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 prints out the size of myfile.txt in bytes.

See also

fseek Reposition stream position indicator (function)
fgetpos Get current position in stream (function)
rewind Set position indicator to the beginning (function)
© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us