Rewind() function in C language
A rewind() function used to reset the cursor pointer at beginning of the file. It is an alternate to fseek() with constant SEEK_SET and offset 0. It can be use to search content in the file and after every search you want to reset cursor position to the beginning of the file
Syntax
 1 rewind(FILE *stream);
stream : It is file or stream variable in which you are going to perform read or write operation. If you want to read from console, you can specify stdin for read or stdout for write.
Reset cursor at beginning of the file using rewind() function
 1 #include <stdio.h>
 2 
 3 int main(){
 4 
 5     FILE *file;
 6     char data[60];
 7     file = fopen ("text.txt","w+");
 8     fputs("Setting current location of the cursor using fseek function !", file);
 9     fflush(file);
 10     
 11     // set cursor to 30th character in the file
 12     fseek(file, 30, SEEK_SET);
 13     fflush(file);
 14     fgets ( data, 60, file );
 15     printf("\nAfter SEEK_SET to 30 : %s", data);
 16     
 17     // set cursor at beginning of the file
 18     rewind(file);
 19     fgets ( data, 60, file );
 20     printf("\nAfter rewind() : %s", data);
 21     
 22     fclose(file);
 23     return 0; 
 24 }
In the above example, we are writing string into the file and using fseek() function, setting current cursor position to 30th character and reading content of file using fgets(). The rewind() function reset the current cursor position to beginning of the file. Now, in the next read operation the content of the file will be read from beginning.
Output
 1 After SEEK_SET to 30 : e cursor using fseek function !
 2 // after rewind function called.
 3 After rewind() : Setting current location of the cursor using fseek function
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us