Easy Tutorial
❮ C Examples Add Matrix C Function Strftime ❯

C Library Function - rewind()

C Standard Library - <stdio.h>

Description

The C library function void rewind(FILE *stream) sets the file position to the beginning of the file for the given stream stream.

Declaration

Following is the declaration for 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 <stdio.h>

int main()
{
   char str[] = "This is tutorialpro.org";
   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;
}

Assuming we have a text file file.txt with the following content:

This is tutorialpro.org

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

This is tutorialpro.org
This is tutorialpro.org

C Standard Library - <stdio.h>

❮ C Examples Add Matrix C Function Strftime ❯