Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Wednesday, 26 March 2014

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