Rewind() function in c: The function void rewind(FILE *stream); sets the file position associated with stream to the beginning of the file.
Function prototype of rewind
void rewind(FILE *stream);
- stream : A pointer to a FILE object which identifies a stream.
Return value of rewind
NONE
C program using rewind function
Rewind function in c: The following program shows the use of rewind function to reset the position indicator of a stream to beginning of the file. Below program first creates a file called textFile.txt with string “TechCrashCourse.com” and then prints the first four characters of the file which is “Tech”. The program uses rewind function to resent the position indicator of stream to beginning of file and then again prints the content of the file.
#include <stdio.h> int main (){ int c, counter = 0; FILE *file; /* Create a new file */ file = fopen("textFile.txt", "w"); fputs("TechCrashCourse.com", file); fclose(file); file = fopen("textFile.txt", "r"); /* First print four characters from a file and then rewind file pointer to point to first character again */ while(!feof(file)){ counter++; c = fgetc(file); printf("%c", c); if(counter == 4){ break; } } /* Rewind file pointer */ rewind(file); printf("\n"); while(!feof(file)){ c = fgetc(file); printf("%c", c); } return(0); }
Output
Tech TechCrashCourse.com