File handling Functions

  • fseek() function
  • rewind() function
  • ftell() function

fseek() function

The fseek() function is used to set the file pointer to specified offset. Write data into file at desired location.

The syntax of the fseek()

int fseek(FILE *stream, long int offset, int whence)

myfile.txt

This is neha jaiswal

rewind() function

The syntax of the rewind()

void rewind(FILE *stream)

let’s see the example of rewind() :

File :file.txt

The technical funda

File : rewind.c

#include<stdio.h>  
#include<conio.h>  
void main(){  
FILE *fp;  
char c;  
clrscr();  
fp=fopen("file.txt","r");  
  
while((c=fgetc(fp))!=EOF){  
printf("%c",c);  
}  
  
rewind(fp); 
  
while((c=fgetc(fp))!=EOF){  
printf("%c",c);  
}  
  
fclose(fp);    
getch();    
}

Output

The technical fundaThe technical funda

ftell() function

The ftell() function return the current file position of the specific stream. We can use SEEK_END constant to move the file pointer at the end of the file.

The Syntax of ftell() function

long int ftell(FILE *stream)

let’s see the example –

File: ftell.c

#include <stdio.h>  
#include <conio.h>  
void main (){  
   FILE *fp;  
   int length;  
   clrscr();  
   fp = fopen("file.txt", "r");  
   fseek(fp, 0, SEEK_END);  
  
   length = ftell(fp);  
  
   fclose(fp);  
   printf("Size of file: %d bytes", length);  
   getch();  
}

Output

Size of file: 21 bytes

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews