Reset file position indicator to start of the file : File Pointer « File « C / ANSI-C






Reset file position indicator to start of the file

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

int main(void)
{
  char str[80];
  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(str);
    strcat(str, "\n");  /* add a newline */
    fputs(str, fp);
  } while(*str!='\n');

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

  return 0;
}

           
       








Related examples in the same category

1.Set the file pointer to the beginning of a stream: how to use rewind
2. Return the current position in a stream: how to use ftell
3.Reposition file pointer to a saved location: how to use fsetpos