A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
After you read through a file, the internal position indicator sits at the end. Any further read returns EOF immediately. rewind(fp) moves that indicator back to byte zero. It is equivalent to fseek(fp, 0, SEEK_SET) but also clears the end-of-file and error indicators. That clearing behavior is the reason rewind exists as a separate function.
#include <stdio.h>int main(void) {FILE *fp = fopen("notes.txt", "r");if (fp == NULL) {perror("fopen");return 1;}printf("First pass:\n");int ch;while ((ch = fgetc(fp)) != EOF) {putchar(ch);}rewind(fp);printf("\nSecond pass (after rewind):\n");while ((ch = fgetc(fp)) != EOF) {putchar(ch);}fclose(fp);return 0;}
Without the rewind call, the second while loop would execute zero iterations because the position indicator is already at EOF. The file has not changed. Your cursor has. rewind moves the cursor. It does not reload the file or allocate memory. It is a cheap operation.
They are almost identical. The one difference: rewind also clears the EOF flag and the error flag on the stream. If a previous read hit EOF, the stream's internal eof indicator is set. fseek alone does not clear it on all implementations. rewind does, per the standard. If you are re-reading after hitting EOF, use rewind to be safe.
rewind is a small function that solves a specific problem cleanly. You will use it in parsers that need multiple passes over input, in test code that verifies file contents, and anywhere you want to re-read without the overhead of close and reopen. Next: fseek, which generalizes repositioning to any offset.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.