Table of Contents

ftell

    long int ftell(FILE *stream);

Returns current file position for stream stream, or -1 on error.

C Sourcecode Example

#include <stdio.h> /* including standard library */
//#include <windows.h> /* uncomment this for Windows */
 
 
int main( void )
{
    FILE *stream;
    int c;
 
    if((stream=fopen("test.txt","r"))==NULL) {
        printf("Cannot open file.\n");
        return 1;
      }
 
    while((c = fgetc(stream) ) != EOF) {
        printf("position of pointer in the stream is %i on character %c\n", ftell(stream), c);
    }
    fclose(stream);
    return 0;
}

content of test.txt

Test 1234

output

  output:  ./ftell 
  position of pointer in the stream is 1 on character T
  position of pointer in the stream is 2 on character E
  position of pointer in the stream is 3 on character s
  position of pointer in the stream is 4 on character t
  position of pointer in the stream is 5 on character 
  position of pointer in the stream is 6 on character 1
  position of pointer in the stream is 7 on character 2
  position of pointer in the stream is 8 on character 3
  position of pointer in the stream is 9 on character 4