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

None.

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)
            putchar(c);
        
        rewind(fp);
        putchar('\n');
        
        while ((c = getc(fp)) != EOF)
            putchar(c);
    }
    fclose(fp);
    return 0;
}

When you run the program, the output will be:

Welcome to Programiz.com
Welcome to Programiz.com