C programming rewind stdio.h

In C programming the <stdio.h> rewind function set the file indicator to the beginning of the file.The declaration of the function is given below.

void rewind(FILE *stream);

stream -The pointer to the file whose indicator is to be set to the beginning of the file.

Return type

Calling ‘rewind’ clears the EOF and error flags or error indicator for the stream.The call ‘rewind’ is also same as,

(void)fseek(stream, 0L, SEEK_SET)

but note the above fssel call does not clear the error indicator for the stream.

Code example

FILE *fp=fopen(“text.txt” , “r+”); //replace text.txt with any of your file
int val , count ;

if (fp != NULL)
while ( (val=fgetc(fp)) != EOF) //read text.txt file
printf(“%c”, val);

if (count == 10)
rewind( fp ); /*clears the current file indicator and set the indicator to the beginning of the file */
 printf(“File cannot be opened”);


//Read the file from the beginning
while( (val = fgetc(fp)) !=EOF )
printf(“%c”, val);

fclose( fp );

If you run the program you will see that the first 10 characters is read and then the file is read from the beginning again.

Related links

->C stdio.h fseek

->C stdio.h ftell

->C stdio.h fsetpos