C Library Function - rewind()
Description
C library function *void rewind(FILE stream) Sets the file position to the beginning of the file for the given stream stream .
Declaration
The following is the declaration of the rewind() function.
void rewind(FILE *stream)
Parameters
- stream -- This is a pointer to a FILE object that identifies the stream.
Return Value
This function does not return any value.
Example
The following example demonstrates the use of the rewind() function.
#include
int main()
{
char str[] = "This is runoob.com";
FILE *fp;
int ch;
/* First, let's write some content to the file */
fp = fopen( "file.txt" , "w" );
fwrite(str , 1 , sizeof(str) , fp );
fclose(fp);
fp = fopen( "file.txt" , "r" );
while(1)
{
ch = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", ch);
}
rewind(fp);
printf("\n");
while(1)
{
ch = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", ch);
}
fclose(fp);
return(0);
}
Suppose we have a text file file.txt, with the following content:
This is runoob.com
Let's compile and run the above program, which will produce the following result:
This is runoob.com
This is runoob.com
C Standard Library -