Pointer to the FILE stream to be reset to the beginning. FILE pointer parameter must be: Valid (not NULL); From successful fopen() call; Not previously closed; File must be seekable (not all streams are);
Read more about parameters of rewind in parameters section
The rewindfunction in C language sets the file position to the beginning of the file stream.
rewind is a stream positioning function used to reset a file stream back to its beginning, commonly used when multiple passes through a file are needed or to restart reading/writing from the start. What makes it special is its automatic clearing of both error and EOF indicators while repositioning, making it simpler to use than fseek for returning to file start. The function works as part of the file positioning family with fseek and ftell, and affects all subsequent I/O operations like fread, fwrite, fprintf, fscanf that depend on the file position indicator.
To Summarize the Workflow:
1.
Takes FILE pointer as single parameter
2.
Sets file position indicator to beginning of file
3.
Clears the error indicator
4.
Clears the EOF indicator
5.
No return value (void function)
6.
Clears all buffers in the process
Read more about return type and value ofrewind function in return section.
The rewindfunction takes 1
parameter:
•
FILE * `stream`: Pointer to the FILE stream to be reset to the beginning. FILE pointer parameter must be: Valid (not NULL); From successful fopen() call; Not previously closed; File must be seekable (not all streams are);
Key effects on parameter stream: Position indicator reset to beginning; Error indicator cleared; EOF indicator cleared; Any pushed-back character (ungetc) is lost; Buffered data is flushed;
The rewind function return value :
This function does not return a value Though rewind() is a void function , there are important return-related considerations: ● No direct way to detect failure ● Must use other functions to check if rewind succeeded: ferror() to check new errors ftell() to verify position is 0 fgetpos() to check position
Important: The fact that it's void means errors must be detected indirectly, which makes it different from similar functions like fseek that return error status directly
Output
This example writes a string to a file, uses rewind to move the file position indicator back to the beginning, and then reads and prints the content.