I have noticed two methods to return to the beginning of a file
FILE *fp = fopen("test.bin", "r")
fseek(fp, 0, SEEK_END);
rewind(fp);
and
FILE *fp = fopen("test.bin", "r")
fseek(fp, 0, SEEK_END);
fseek(fp, 0, SEEK_SET);
What would be difference if any between these methods?
They are basically two different ways to accomplish the same thing: set the pointer to the beginning of the file. The only difference is that rewind
also clears the error indicator.
If given the choice, you should use fseek
. This is because rewind
doesn't return an integer indicating whether the operation has succeeded.
If fseek()
returns success, it will also clear the end-of-file indicator, whereas rewind()
does not do so
来源:https://stackoverflow.com/questions/11839025/fseek-vs-rewind