Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Syntax of the Function
2.
Working of Rewind() Function in C
3.
Example of Rewind() Function
3.1.
Input: 
3.2.
Output:
4.
Frequently Asked Questions
4.1.
Why use rewind in C?
4.2.
How to rewind a pointer in C?
4.3.
What does rewind () mean in C++?
5.
Conclusion
Last Updated: Jun 7, 2024
Easy

Rewind() Function in C

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Rewind() function in C returns the file position indicator to the beginning of the file. It takes a file pointer as input and moves the indicator to the beginning of the file, allowing data to be read once more from the beginning.

The rewind() function is a standard inbuilt function of the C Programming Language. Its purpose is to set the file pointer at the starting of the stream so that the stream can be used as many times as a programmer wants. This function also terminates all the errors present in the file and specifies all the insignificant functions at the end of the file pointed by indicators present in the stream.

Rewind Function in C

Recommended Topics, Sum of Digits in C and C Static Function.

Syntax of the Function

Syntax of Rewind() function is:

void rewind(FILE*stream);

 

Syntax of the rewind() function is the procedure through which we pass parameters to the function. Parameters of the rewind() function are described below:
 

  • File: It represents the file that is being pointed by the indicator.

 

  • Stream: A stream is a file pointer that indicates the file from the beginning or from the end by removing all the insignificant functions or data.

 

Rewind() function is of void data type and does not return any value. Its only purpose is to set file position.

 

Click on the following link to read further: Features of C Language, and  Tribonacci Series

 

Working of Rewind() Function in C

The rewind() function is also an inbuilt standard function that consists of two elements: file functions and stream. Both of these functions are entirely interdependent, meaning both functions will collectively perform the task. The rewind () function facilitates the programmers to work with internal pointers to indicate the data stream and the file. Given below are some easily explained points which describe how the rewind() function works:

  • File and file objects are the two parameters on which the rewind() function operates where objects point to the stream.

 

  • Rewind() function provides the potential to parameters that will always place the file in the starting position and towards which stream will indicate.
     
  • Suppose the file contains any errors or irrelevant information or data unrelated to the topic, then it will not work in the same way as it should because it has the special ability to terminate the errors of irrelevant data from the file. This unique feature helps programmers to work efficiently.
     
  • The primary function of the stream pointer is to put and fix the file at starting position.
     
  • The data type of the rewind() function is void, which means it does not return any type of data.

 

  • Different libraries store different functions in the C programming language, also known as headers. To use those functions, we first need to put the library/header file related to that function at the starting of the program so that the compiler can understand the meaning of that function. Rewind() function is the function of a library/header file named as <stdio.h>.
     
  • The main functionalities of rewind() functions work only with some particular versions like ANSI/ISO 9899-1990.
     
  • Some other functions which act similarly like the rewind() function are: fseek() function and fsetpos() function. They have minimal differences in their functionalities.
     
  • The time complexity of the rewind() function is 0, which means this function does not have any time complexity, and it is also not a very frequently used function.

 

Must Read Decision Making in C

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example of Rewind() Function

Suppose there is a file whose name is myfile.txt that has the following content:

#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); //puts the file pointer at the starting of the file.
  while((b=fgetc(flptr))!=EOF){
    printf("%c",b);
  }
  fclose(flptr);
  getch();
}

 

Input: 

Hello, I am Gaurav Bashisht. I am a Technical Blog Writer at Coding Ninjas.

 

Output:

Hello, I am Gaurav Bashisht. I am a Technical Blog Writer at Coding Ninjas.
Hello, I am Gaurav Bashisht. I am a Technical Blog Writer at Coding Ninjas.

 

The output will be two times the content in the file because the second sentence is the original content, and the first sentence is the repositioned file by the rewind() function. 

You can also read about dynamic array in c and Short int in C Programming

Must Read what is storage class in c

Frequently Asked Questions

Why use rewind in C?

In C, we use Rewind() to read the contents of a file from the beginning again. By resetting the file position indicator to the beginning of the file, we can read data from the beginning of the file without closing and reopening it.

How to rewind a pointer in C?

In C, the rewind() method can rewind a pointer. It accepts a file pointer as an argument and resets the position indicator to the beginning of the file, allowing us to read the file from the beginning once more.

What does rewind () mean in C++?

The rewind() method is used to return the file position indicator to the file's beginning. It is part of the C standard library and can be used to read the contents of a file from start to finish without having to reopen it.

Conclusion

The rewind() function is a standard library function of the C language used most frequently during file handling. Its input or output stream with pointers points to some dataset to be repositioned in the file appropriately. Rewind() Function provides flexibility to programmers.

Also, read the HCF of two numbers using CC program to concatenate two strings.

Refer to our guided paths on Coding Ninjas Studio to learn more about C programming, DSA, JavaScript, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass