ftell() and fseek() In C Language

ftell() :

Functions ftell() and fseek() are important in a program performing file manipulations. Function ftell() returns the current position of the file pointer in a stream. The return value is 0 or a positive integer indicating the byte offset from the beginning of an open file. A return value of -1 indicates an error. Prototype of this function is as shown below:

     long int ftell(FILE *fp);

fseek() :

This function positions the next I/O operation on an open stream to a new position relative to the current position.

     int fseek(FILE *fp, long int offset, int origin);

Here fp is the file pointer of the stream on which I/O operations are carried on; offset is the number of bytes to skip over. The offset can be either positive or negative, denting forward or backward movement in the file. Origin is the position in the stream to which the offset is applied; this can be one of the following constants:

    SEEK_SET :  offset is relative to beginning of the file
    SEEK_CUR :  offset is relative to the current position in the file
      SEEK_END :  offset is relative to end of the file
  Program : The following program is an example of ftell() and fseek().
#include<stdio.h>
main()
 { 
FILE *fp; 
char ch; 
fp=fopen(“fileName.txt” ,”r”);
 fseek( fp,21 ,SEEK_SET); 
 ch=fgetc(fp);  
while(!feof(fp ))  
{ 
printf(“%c” ,ch);
 printf(“%d”, ftell(fp ));
  ch= getc(fp );  
} 
rewind(fp );
 while(!feof(fp)) 
{  
printf(“%c” ,ch);
 printf(“%d” ,ftell(fp));
  ch= fgetc(fp); 
} 
fclose(fp ); 
}
Output :

0H1e2l3l4o5     6I7n8dgi10a11