C rewind() function

The rewind() function is a standard library function in the C programming language that is used to reset the file position indicator to the beginning of a file. It is declared in the <stdio.h> header file.

Here is the syntax of the rewind() function:

void rewind(FILE *stream);

The rewind() function takes a pointer to a FILE object as its argument. This FILE object should be associated with the file you want to reset the position indicator for.

When you call rewind(), it clears the error and end-of-file indicators for the given file stream and moves the file position indicator to the beginning of the file. This means that subsequent read or write operations on the file will start from the beginning.

Here’s an example usage of the rewind() function:

#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    // Read data from the file...

    rewind(file);  // Reset the file position indicator to the beginning

    // Read the file from the beginning again...

    fclose(file);
    return 0;
}

In this example, we open a file named “example.txt” for reading. After reading some data from the file, we call rewind(file) to reset the file position indicator to the beginning. This allows us to read the file again from the start.

It’s important to note that rewind() does not clear the file’s contents or truncate the file. It only affects the file position indicator used for subsequent read or write operations.