Rewind( ) function resets the file position indicator to the beginning of the file - C File

C examples for File:File Operation

Introduction

rewind( ) function prototype is

void rewind(FILE *fp);

where fp is a valid file pointer.

Demo Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
   char str[80];/*from ww  w  . ja  v a2s.c  o m*/
   FILE *fp;

   if ((fp = fopen("TEST", "w+")) == NULL) {
      printf("Cannot open file.\n");
      exit(1);
   }

   do {
      printf("Enter a string (CR to quit):\n");
      gets_s(str);
      strcat(str, "\n"); /* add a newline */
      fputs(str, fp);
   } while (*str != '\n');

   /* now, read and display the file */
   rewind(fp);  /* reset file position indicator to
                start of the file. */
   while (!feof(fp)) {
      fgets(str, 79, fp);
      printf(str);
   }

   return 0;
}

Result


Related Tutorials