In C Language, rewind() is a standard library function used to reset the file position indicator to the beginning of a file.
It is equivalent to calling "fseek()" with an offset of 0 bytes from the beginning of the file.
void rewind(FILE *stream);
"stream": Pointer to a "FILE" object that identifies the file.
The "rewind()" function does not return a value.
"rewind()" is used to reset the file position indicator so that the file can be read again.
Note: "rewind()" is called after the first loop to reset the file position indicator. Then, the file is read again from the beginning.
#include <stdio.h> int main() { FILE *file; char ch; // Open the file in read mode file = fopen("example.txt", "r"); // Check if file opened successfully if (file == NULL) { perror("Error opening file"); return 1; } // Read and print characters from the file while ((ch = fgetc(file)) != EOF) { putchar(ch); } // Reset the file position indicator to the beginning of the file rewind(file); // Read and print characters from the beginning of the file again printf("\nReading from the beginning again:\n"); while ((ch = fgetc(file)) != EOF) { putchar(ch); } // Close the file fclose(file); return 0; }
#include <stdio.h> int main() { FILE *file; // Open the file in write mode file = fopen("output.txt", "w"); // Check if file opened successfully if (file == NULL) { perror("Error opening file"); return 1; } // Write content to the file fprintf(file, "Hello, world!\n"); // Reset the file position indicator to the beginning of the file rewind(file); // Read and print content from the file printf("Content of the file:\n"); char ch; while ((ch = fgetc(file)) != EOF) { putchar(ch); } // Close the file fclose(file); return 0; }
"rewind()" is used to reset the file position indicator after writing to a file so that the written content can be read:
"rewind()" is used after writing to the file to reset the file position indicator. Then, the content of the file is read and printed to the console.