rewind() Function
The rewind()
function in C stdio.h resets the position indicator for a given file stream, effectively setting it back to the beginning of the file. This is useful for re-reading a file or clearing any previous error or end-of-file indicators, and it facilitates switching between reading and writing on update streams.
Syntax of rewind()
void rewind(FILE *stream);
Parameters
Parameter | Description |
---|---|
stream | A pointer to a FILE object that identifies the stream. |
After a successful call, the rewind()
function clears the error and end-of-file indicators for the stream and discards any effects from previous calls to ungetc()
. It also enables switching between reading and writing in streams opened for update.
Return Value
The rewind()
function does not return any value.
Examples for rewind()
Example 1: Basic Usage of rewind() to Reset File Stream
This example demonstrates how to use rewind()
to reset the file pointer, allowing a file to be read from the beginning after initial reading.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
char buffer[50];
// Read first line from the file
if (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("First read: %s", buffer);
}
// Reset file pointer to the beginning
rewind(fp);
// Read the first line again after rewinding
if (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("After rewind: %s", buffer);
}
fclose(fp);
return 0;
}
Explanation:
- The program opens a file named
"example.txt"
for reading. - It reads the first line and prints it.
- The
rewind()
function is called to reset the file pointer to the beginning. - The first line is read again and printed, demonstrating that the stream position was successfully reset.
Program Output:
First read: This is the first line of the file.
After rewind: This is the first line of the file.
Example 2: Switching Between Reading and Writing Modes
This example shows how rewind()
can be used to switch from reading to writing on a file opened for update.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("update.txt", "w+");
if (fp == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
// Write data to the file
fputs("Initial Data\n", fp);
// Rewind to read what was written
rewind(fp);
char buffer[50];
if (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("Data after rewind: %s", buffer);
}
fclose(fp);
return 0;
}
Explanation:
- A file
"update.txt"
is opened in update mode ("w+"
). - The program writes some initial data to the file.
- The
rewind()
function resets the stream to the beginning. - The program reads the written data from the start and prints it.
Program Output:
Data after rewind: Initial Data
Example 3: Clearing Error and EOF Indicators
This example illustrates how rewind()
clears the end-of-file and error indicators, allowing further operations on the stream.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
// Move to the end to simulate EOF
fseek(fp, 0, SEEK_END);
if (feof(fp)) {
printf("Reached end-of-file.\n");
}
// Now rewind the file to clear EOF indicator
rewind(fp);
if (!feof(fp)) {
printf("EOF indicator cleared after rewind.\n");
}
fclose(fp);
return 0;
}
Explanation:
- The file
"data.txt"
is opened for reading. - The file pointer is moved to the end of the file using
fseek()
, setting the EOF indicator. - The program checks and confirms that the end-of-file has been reached.
- The
rewind()
function is called, which clears the EOF (and error) indicator. - A subsequent check confirms that the EOF indicator has been cleared.
Program Output:
Reached end-of-file.
EOF indicator cleared after rewind.