Search This Blog

Loading...

ILLUSTRATION OF fseek & ftell FUNCTIONS

//write a program that uses the functions ftell and fseek.
//Code for ILLUSTRATION OF fseek & ftell FUNCTIONS in C Programming

#include <stdio.h>                                            
   main()                                                          
   {                                                               
       FILE  *fp;                                                  
       long  n;                                                    
       char c;                                                     
       fp = fopen("RANDOM", "w");                                  
       while((c = getchar()) != EOF)                               
           putc(c,fp);                                             
                                                                   
       printf("No. of characters entered = %ld\n", ftell(fp));     
       fclose(fp);                                                 
       fp = fopen("RANDOM","r");                                   
       n = 0L;                                                     
                                                                   
       while(feof(fp) == 0)                                        
       {                                                           
           fseek(fp, n, 0);  /*  Position to (n+1)th character */
  
           printf("Position of %c is %ld\n", getc(fp),ftell(fp)); 
           n = n+5L;                                               
       }                                                           
       putchar('\n');                                              
                                                                   
       fseek(fp,-1L,2);     /*  Position to the last character */
do                                                          
         {                                                         
             putchar(getc(fp));                                    
         }                                                         
         while(!fseek(fp,-2L,1));                                    
         fclose(fp);                                                 
   }                                                               

Output:
   ABCDEFGHIJKLMNOPQRSTUVWXYZ^Z                                    
   No. of characters entered = 26                                  
   Position of A is 0                                              
   Position of F is 5                                              
   Position of K is 10                                             
   Position of P is 15                                             
   Position of U is 20                                             
   Position of Z is 25                                             
   Position of   is 30                                             
                                                                   
   ZYXWVUTSRQPONMLKJIHGFEDCBA                                      

No comments:

Post a Comment