Mega Code Archive

 
Categories / C / Code Snippets
 

Reset file position indicator to start of the file

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char str[100]; FILE *filep; if((filep = 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, filep); } while(*str!='\n'); rewind(filep); /* reset file position indicator to start of the file. */ while(!feof(filep)) { fgets(str, 99, filep); printf(str); } return 0; }