Join our newsletter for the latest updates.

C++ rewind()

The rewind() function in C++ sets the file position indicator to the beginning of the given file stream.

rewind() prototype

void rewind(FILE* stream);

A call to rewind(stream) is equivalent to a call to fseek(stream, 0, SEEK_SET), except that end-of-file and error indicators are cleared.

It is defined in <cstdio> header file.

rewind() Parameters

stream: The file stream to reset the error flags and EOF indicator.

rewind() Return value


Example: How rewind() function works

#include <cstdio>

int main()
    int c;
    FILE *fp;
    fp = fopen("file.txt", "r");
    if (fp)
        while ((c = getc(fp)) != EOF)
        while ((c = getc(fp)) != EOF)
    return 0;

When you run the program, the output will be:

Welcome to
Welcome to
Did you find this article helpful?