fseek vs rewind?
Asked Answered
C

2

20

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?

Commotion answered 7/8, 2012 at 3:54 Comment(0)
D
24

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.

Differentiable answered 7/8, 2012 at 4:0 Comment(0)
R
4

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

Roesch answered 21/12, 2012 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.