C Rewind Function

It has the following syntax:

Example

void rewind(FILE *stream)

In this syntax,

  • FILE: It represents the file towards which the indicator will point.
  • *stream: It represents a pointer to the FILE object that shows the file whose pointer will be reset.
  • Return Type: It doesn't provide any return value.
  • How the rewind Function Works in C?

In C programming, the rewind function is a built-in function commonly used to reposition the file stream pointer back to the initial position within the file. This functionality allows a program to access the file's content from the beginning without requiring a new file opening operation. Additionally, it serves to clear any error or end-of-file indicators that may be linked to the stream.

The rewind function helps to perform several operations by going through this set of statements and functions:

  • The rewind function treats the parameter as a file and the file object as an object referencing the stream.
  • Being part of the standard library, it allows the stream to position the file pointer at the beginning of the file, which enables the operations to start from the top.
  • If the file stream contains unwanted or incorrect information, the rewind function can reset the file position to the beginning. However, it does not delete or erase any data; but it simply repositions the file pointer, which allows the programmer to reread or overwrite the content from the start.
  • The stream pointer available in the function ensures that the file is positioned at the start of the file.
  • The fsetpos function is mainly utilized to position the file object to a certain location in the file. It accepts a file position object (fpos_t) as an argument, which designates the targeted position in the file. This function supports exact positioning within the file according to the given position. This function also possesses a return type that the rewind function never accommodates.
  • C Example for rewind function

Let's consider a scenario to demonstrate the rewind function in the C programming language.

File: file.txt

Example

Hello!

Welcome to Logic Practice

File: rewind.c

Example

Example

#include <stdio.h>



int main() {    //main function

    FILE *fp;

    char c;



    fp = fopen("file.txt", "r"); // Open file in read mode

    if (fp == NULL) {

        printf("Error: Cannot open file.\n");

        return 1;

    }



    // First read: display file content

    printf("Reading file the first time:\n");

    while ((c = fgetc(fp)) != EOF) {

        putchar(c);

    }



    // Rewind file pointer to beginning

    rewind(fp);



    // Second read: display file content again

    printf("\n\nReading file the second time after rewind():\n");

    while ((c = fgetc(fp)) != EOF) {

        putchar(c);

    }



    fclose(fp); // Close file

    return 0;

}    

</stdio.h>

Output:

Output

Reading file the first time:

Hello!

Welcome to Logic Practice



Reading file the second time after rewind():

Hello!

Welcome to Logic Practice

Explanation:

The rewind method relocates the file pointer to the start of the file. This explains why the phrase "Hello! Welcome to Logic Practice" is displayed twice. Omitting the rewind call would result in the phrase being printed just once.

In this instance, we showcase the utilization of the rewind function in the C programming language. Initially, it opens a file named file.txt in read mode, reads the content, and presents it once. Subsequently, the rewind(fp) function is employed to reset the file pointer back to the starting position. Ultimately, the file content is reread and displayed from the beginning.

C Example for rewind function using fseek and fsetpos function

In this example, we demonstrate how the fseek and fsetpos functions differ from the rewind function. While all three are used to reposition the file pointer within a stream, they serve distinct purposes. The rewind function simply resets the file position to the beginning, whereas the fseek and fsetpos functions offer greater control by enabling movement to arbitrary locations within the file.

File: file.txt

Example

Hello!

Welcome to Logic Practice

File: rewind.c

Example

Example

#include <stdio.h>



int main() {

    FILE *fp;

    fpos_t position;



    // Open the file in read mode

    fp = fopen("file.txt", "r");

    if (fp == NULL) {

        perror("Error opening file");

        return 1;

    }



    // Save the current file position

    if (fgetpos(fp, &position) != 0) {

        perror("Error getting file position");

        fclose(fp);

        return 1;

    }



    //using rewind()

    printf("\nUsing rewind():\n");

    rewind(fp);

    for (int i = 0; i < 5; i++) {

        int ch = fgetc(fp);

        if (ch == EOF) break;

        putchar(ch);

    }



    // using fseek()

    printf("\nUsing fseek():\n");

    if (fseek(fp, 5, SEEK_SET) != 0) {

        perror("Error using fseek");

        fclose(fp);

        return 1;

    }

    for (int i = 0; i < 5; i++) {

        int ch = fgetc(fp);

        if (ch == EOF) break;

        putchar(ch);

    }



    // using fsetpos()

    printf("\nUsing fsetpos():\n");

    if (fsetpos(fp, &position) != 0) {

        perror("Error setting file position");

        fclose(fp);

        return 1;

    }

    for (int i = 0; i < 5; i++) {

        int ch = fgetc(fp);

        if (ch == EOF) break;

        putchar(ch);

    }



    // Close the file

    fclose(fp);

    return 0;

}

Output:

Output

Using rewind():

Hello

Using fseek():

!

Wel

Using fsetpos():

Hello

Explanation:

In this instance, we begin by opening a file named file.txt in read mode and capturing the current location using fgetpos. Subsequently, it demonstrates the functionality of rewind which resets the file pointer to the start, fseek which moves it to the 5th character, and fsetpos which reverts it to the saved position. Each time, it retrieves and displays five characters from the saved position.

rewind function FAQs

The rewind function in C is utilized to reset the file position indicator to the beginning of a file.

In C programming, the rewind function resets the file position to the beginning of the file and clears any error or end-of-file indicators.

2) What is the format of the rewind function?

The format of the rewind function in the C programming language is demonstrated below:

Example

void rewind(FILE *stream);

The difference between the rewind function and the fseek function lies in their behavior related to file positioning. While rewind sets the file position indicator to the beginning of the file, fseek allows for more flexibility by moving the file position indicator to a specified location within the file.

In C programming, utilizing rewind will consistently move the file pointer to the beginning and reset error flags. In contrast, fseek allows for navigation to any desired position but does not automatically reset error flags.

4) Is it possible to utilize the rewind function on files that have been opened in either write or append mode?

Yes, the rewind function is applicable to files that are open in either write or append mode, although its behavior may become unpredictable if invoked after writing without performing a flush operation.

5) Does the rewind function return a value?

No, the rewind method has a void return type and does not provide a status value.

Input Required

This code uses input(). Please provide values below: