Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does rewind() do?

Tags:

c

rewind

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?

like image 252
Simplicity Avatar asked Oct 17 '25 21:10

Simplicity


1 Answers

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.

like image 132
dbush Avatar answered Oct 20 '25 12:10

dbush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!