rewind() in C programming

File Handling: rewind() in C programming

File Handling is a very useful operation in C programming as it help to curb with various insufficiency and error of normal limitation of stdout console in the language. Some insufficiency are, unable to display large size of data in the stdout console. There are various functions in File handling and here we will talk about rewind() in C programming.

rewind-in-C-programming

rewind() in C programming

rewind() function  is used to set the file pointer or indicator associated with the stream to the beginning of  the given stream or file.

Syntax:

void rewind(FILE*stream);

Explanation:

  • File- it is used to represent the file that is pointed by the indicator and specifies that it is open.
  • stream- it is a pointer to the file that identifies the stream for which a memory is associated with it.

Return Value:

  • rewind()- it does not return any value.

Also there is no upfront way to check is rewind() function worked in your program or not.

rewind()-in-c-programming-meme

Example 1:

Run
#include<stdio.h>
int main()
{
    int i;
    FILE *rewi;
    rewi = fopen("file.txt", "r");
    if (rewi)
    {
        while ((i = getc(rewi)) != EOF)
        {
            putchar(i);
        }
        rewind(rewi);
        putchar('\n');
        while ((i = getc(rewi)) != EOF)
        {
            putchar(i);
        }
    }
    fclose(rewi);
    return 0;
}

Output:

File opened successfully File opened successfully

Example 2:

Run
#include <stdio.h> 
#include <conio.h> 
void main()
{
    FILE *flptr;  
    char b;
    clrscr();  
    flptr = fopen("myfile.txt","r");
    while((c = fgetc(flptr)) != EOF)
    {  
        printf("%c",b);  
    }  
    rewind(flptr); 
    while((b=fgetc(flptr))!=EOF)
    {   
        printf("%c",b);
    }
    fclose(flptr);
    getch();
}

Input:

Here to help at PrepInsta

Output:

Here to help at PrepInsta
Here to help at PrepInsta

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription