Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday 1 June 2019

C rewind() function


The rewind() function sets the file pointer at the beginning of the stream. It is useful if you have to use the stream many times.

Syntax:
void rewind(FILE *stream)  

Example:

File: file.txt

this is a simple text  
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);//moves the file pointer at beginning of the file  
  
while((c=fgetc(fp))!=EOF){  
printf("%c",c);  
}  
  
fclose(fp);    
getch();    
}    

Output:

this is a simple textthis is a simple text

As you can see, rewind() function moves the file pointer at the beginning of the file that is why "this is simple text" is printed 2 times. If you don't call rewind() function, "this is simple text" will be printed only once.

0 comments:

Post a Comment