rewind - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# rewind

From cppreference.com
< c‎ | io

Defined in header `<stdio.h>` |  |
void rewind( FILE *stream ); |  |

Moves the file position indicator to the beginning of the given file stream.

The function is equivalent to fseek(stream, 0, SEEK_SET);, except that end-of-file and error indicators are cleared.

The function drops any effects from previous calls to ungetc.

### [edit] Parameters

stream | - | file stream to modify

### [edit] Return value

(none)

### [edit] Example

This example shows how to read a file twice

Run this code

    #include <stdio.h>

    char str[20];

    int main(void)
    {
        FILE *f;
        char ch;

        f = fopen("file.txt", "w");
        for (ch = '0'; ch <= '9'; ch++) {
            fputc(ch, f);
        }
        fclose(f);

        f = fopen("file.txt", "r");
        fread(str, 1, 10, f);
        puts(str);

        rewind(f);
        fread(str, 1, 10, f);
        puts(str);
        fclose(f);

        return 0;
    }

Output:

    0123456789
    0123456789

### [edit] See also

fseek | moves the file position indicator to a specific location in a file (function) [edit]
C++ documentation for rewind

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/io/rewind&oldid=71205"