6.18.39 rewind Function
Resets the file pointer to the beginning of the file.
Attention: This function is not
implemented by MPLAB XC8.
Include
<stdio.h>
Prototype
void rewind(FILE * stream);
Argument
stream
- stream to reset the file pointer
Remarks
The function calls fseek(stream, 0L, SEEK_SET)
and then clears the error
indicator for the given stream.
Example
#include <stdio.h>
int main(void)
{
FILE *myfile;
char s[] = "cookies";
int x = 10;
if ((myfile = fopen("afile", "w+")) == NULL)
printf("Cannot open afile\n");
else
{
fprintf(myfile, "%d %s", x, s);
printf("I have %d %s.\n", x, s);
/* set pointer to beginning of file */
rewind(myfile);
fscanf(myfile, "%d %s", &x, &s);
printf("I ate %d %s.\n", x, s);
fclose(myfile);
}
}
Example Output
I have 10 cookies.
I ate 10 cookies.