17

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?

Steven Penny
  • 82,115
  • 47
  • 308
  • 348

2 Answers2

20

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.

3

If fseek() returns success, it will also clear the end-of-file indicator, whereas rewind() does not do so

Phileo99
  • 5,206
  • 2
  • 45
  • 52