C library function - rewind()
Advertisements
Description
The C library function void rewind(FILE *stream) sets the file position to the beginning of the file of the given stream.
Declaration
Following is the declaration for rewind() function.
void rewind(FILE *stream)
Parameters
stream -- This is the pointer to a FILE object that identifies the stream.
Return Value
This function does not return any value.
Example
The following example shows the usage of rewind() function.
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
fp = fopen("file.txt", "r");
if( fp != NULL )
{
while( !feof(fp) )
{
ch = fgetc(fp);
printf("%c", ch);
}
rewind(fp);
while( !feof(fp) )
{
ch = fgetc(fp);
printf("%c", ch);
}
fclose(fp);
}
return(0);
}
Assuming we have a text file file.txt having the following content:
This is tutorialspoint.com
Now let us compile and run the above program, this will produce the following result:
This is tutorialspoint.com This is tutorialspoint.com