The rewind() function is a standard library function declared in the <stdio.h> header file. It is used to move the file position indicator back to the beginning of a file without closing and reopening it.
- It resets the file pointer to the start of the file, allowing the file to be read or written again from the beginning.
- It also clears the end-of-file (EOF) and error indicators associated with the file stream.
- It is commonly used when the same file needs to be processed multiple times within a program.
#include <stdio.h>
int main() {
FILE *fptr = fopen("file.txt", "w+");
// Write data to file
fprintf(fptr, "Hello, GFG!");
// Rewind fptr to the beginning
rewind(fptr);
// Read data from file
char ch;
while ((ch = fgetc(fptr)) != EOF) {
printf("%c", ch);
}
fclose(fptr);
return 0;
}
Output
Hello, GFG!
Explanation
- fprintf() first writes "Hello, GFG!" to the file, which moves the file pointer to the end of the written content.
- rewind() resets the file pointer to the beginning of the file, allowing fgetc() to read the file from the start one character at a time.
Syntax
void rewind(FILE *stream);
Parameters
- stream: Pointer to the file whose position indicator is to be reset.
Return Value
- The
rewind()function does not return any value (void). - After execution, the file position is reset to the beginning, and the EOF and error flags are cleared.
Working
The rewind() function resets the file position indicator to the beginning of the specified file.
- Open or access a file using a valid FILE* pointer.
- Perform read or write operations, which move the file pointer from its initial position.
- Call rewind() to move the file pointer back to the beginning of the file.
- Continue reading from or writing to the file without reopening it.
Advantages
The rewind() function provides a simple way to reuse an already opened file.
- Resets the file pointer to the beginning without reopening the file.
- Clears the end-of-file (EOF) and error indicators automatically.
- Useful when the same file needs to be read or processed multiple times.
Limitations
Although rewind() is convenient, it has a few limitations.
- It always resets the file pointer to the beginning and cannot move to an arbitrary position.
- It does not return a status value, so errors cannot be detected directly.
- It works only with valid file streams opened using file handling functions.
rewind() vs fseek()
| rewind() | fseek() |
|---|---|
| Resets the file pointer to the beginning of the file. | Moves the file pointer to any specified position in the file. |
| Does not return any value (void). | Returns 0 on success and a non-zero value on failure. |
| Automatically clears the EOF and error indicators. | Does not clear the EOF indicator; use clearerr() if required. |
| Can only move the file pointer to the beginning of the file. | Can move the file pointer relative to the beginning, current position, or end of the file. |
| Simpler to use when restarting file operations. | More flexible for random file access and precise positioning. |