Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - rewind()

C Library Function - rewind()

C Standard Library - <stdio.h> C Standard Library -

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

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 - <stdio.h> C Standard Library -