What exactly does rewind() do?

2.4k views Asked by At

I came across the rewind() function in C. I went through its description and example from here.

The description mentioned the following about the function:

The C library function void rewind(FILE *stream) sets the file position to the beginning of the file of the given stream.

I really didn't get the idea clear yet. Can we imagine it as a cursor moving in the file to be read, and rewind() simply sets that cursor to the beginning of the file?

1

There are 1 answers

2
dbush On BEST ANSWER

From the man page:

The rewind() function sets the file position indicator for the stream pointed to by stream to the beginning of the file. It is equivalent to:

      (void)fseek(stream, 0L, SEEK_SET)

except that the error indicator for the stream is also cleared (see clearerr(3)).

So the next time you read from a file after calling rewind, you start reading from the beginning. So your cursor analogy is a valid one.