Difference between fseek() and rewind() in C
Following are some differences between fseek() and rewind():
fseek |
rewind |
---|---|
fseek is used to move the file pointer to a specific position. | rewind is used to move the file pointer to the beginning of the file stream. |
Syntax: int fseek(FILE *stream, long offset, int origin); |
Syntax: void rewind(FILE *stream); |
It returns 0 if successful, and a non-zero value if an error occurs. | It does not return any value. |
The stream error indicator is not cleared. | The stream error indicator is cleared. |
Example: fseek(file, 10, SEEK_SET); It moves the file pointer 10 bytes from the beginning of the file. |
Example: rewind(file); It moves the file pointer to the beginning of the file. |
fseek() vs rewind() in C
fseek() and rewind() both functions are used to file positioning defined in <stdio.h> header file. In this article, we will discuss the differences in the usage and behavior of both functions.
fseek() Function
fseek() is used to set the file position indicator to a specified offset from the specified origin.
Syntax
int fseek(FILE *stream, long offset, int origin);
Parameters
- stream: It is a pointer to the FILE stream.
- offset: It specifies the number of bytes to offset from the origin.
- origin: It indicates the position from where the offset is calculated. It can be one of the following:
- SEEK_SET: Beginning of the file.
- SEEK_CUR: Current position of the file pointer.
- SEEK_END: End of the file.
Return Value
- It returns 0 if successful, and a non-zero value if an error occurs.
rewind() Function
rewind() is used to move the file pointer to the beginning of the file stream.
Syntax
void rewind(FILE *stream);
Parameters
- stream: It is the pointer to the file stream
Return Value
- It does not return any value.