Resets the file pointer to the beginning of the file.
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> /* for rewind, fopen, */
/* fscanf, fclose, */
/* fprintf, printf, */
/* FILE, NULL */
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.