2

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?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 4
    It's the same as `fseek(stream, 0, SEEK_SET)`, and does exactly what you expect it to do. See also e.g. [this `rewind` reference](http://en.cppreference.com/w/c/io/rewind), or [this `fseek` reference](http://en.cppreference.com/w/c/io/fseek). – Some programmer dude Aug 06 '15 at 14:56
  • 3
    basically the answer is yes – x4rf41 Aug 06 '15 at 14:57
  • 4
    Your explanation of a cursor sounds pretty good to me - `fseek` is the function to position that cursor more freely. – kratenko Aug 06 '15 at 14:57
  • 4
    Have you ever seen a magnetic tape? – n. m. could be an AI Aug 06 '15 at 15:06
  • 1
    It works like `fseek`, an important distinction is that it does not provide an error code, so you will not know if it failed. Not all streams can be seeked or rewound: usually `stdin` can't be. – Veltas Aug 06 '15 at 15:13

1 Answers1

5

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.

dbush
  • 205,898
  • 23
  • 218
  • 273