C Library Functions -rewind()

C Standard Library - <stdio.h>

Description

C Library Functionsvoid rewind(FILE *stream)Set the file position to the given streamstreamThe beginning of the file.

Declaration

Below is the declaration of the rewind() function.

void rewind(FILE *stream)

Parameter

  • 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 usage of the rewind() function.

#include <stdio.h>

int main()
{
   char str[] = "This is example.com";
   FILE *fp;
   int ch;

   /* 首先让我们在文件中写入一些内容 */
   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 filefile.txt, whose contents are as follows:

This is example.com

Let us compile and run the above program, which will produce the following result:

This is example.com
This is example.com

C Standard Library - <stdio.h>

other extensions