fseek vs rewind?

Steven Penny picture Steven Penny · Aug 7, 2012 · Viewed 10.7k times · Source

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?

Answer

user672118 picture user672118 · Aug 7, 2012

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.