What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

rewind(stream) moves a C file stream back to the beginning and clears its end-of-file and error indicators. It returns no value, so it is useful when you simply need to read a stream again from the start—not when you need a reported success status or a different destination.

What rewind() does

Declare rewind() by including <stdio.h>. Its signature is void rewind(FILE *stream);. The function moves the file position indicator for stream to the beginning of the file. It also clears the stream’s end-of-file and error indicators, and discards the effects of prior ungetc() calls. The C library reference documents these effects.

Clearing the end-of-file indicator matters after a read reaches the end: a subsequent read can proceed from the beginning rather than immediately reporting end-of-file again.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read a file from the beginning again

Keep the stream open, call rewind(stream) after the first pass, then perform the second pass with your usual input function. Check that fopen() succeeded before using the stream and close it when finished.

#include <stdio.h>

int main(void)
{
    FILE *stream = fopen("input.txt", "r");
    if (stream == NULL) {
        perror("fopen");
        return 1;
    }

    /* Read or process the stream here. */

    rewind(stream);
    /* Read or process the stream again from the beginning. */

    if (fclose(stream) == EOF) {
        perror("fclose");
        return 1;
    }
    return 0;
}

The call has no return value to test. The example shows the intended positioning operation; it is not a guarantee that repositioning succeeds in every stream or platform situation.

rewind() vs. fseek()

Both functions can position a stream at its beginning: use rewind(stream) or fseek(stream, 0L, SEEK_SET). Choose between them based on whether you need a fixed destination and whether the program needs status reporting.

Function Positioning Return and error handling Indicator effects
rewind(stream) Beginning of the file only Returns void; no return status to check Clears the end-of-file and error indicators; discards prior ungetc() effects, as described by the C library reference
fseek(stream, offset, whence) Uses a long offset with SEEK_SET (beginning), SEEK_CUR (current position), or SEEK_END (end) Returns a status that the program can check POSIX describes rewind() as equivalent to fseek(stream, 0L, SEEK_SET) except that rewind() also clears the error indicator. POSIX.1-2017

Use rewind() when the beginning is the only destination you need. Use fseek() when you need an offset or want its return status. The standard positioning origins and fseek() interface are described in the C library reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Can rewind() report an error?

No. Its return type is void, so code such as if (rewind(stream) != 0) is invalid. POSIX.1-2017 gives a platform-specific approach for applications that need to detect an error: clear errno before calling rewind(), then treat nonzero errno afterward as an indication of an error. This is not a return-value check and should not be presented as portable ISO C behavior. See the POSIX specification.

Best Value