Updated May 17, 2023
Definition of C rewind()
The rewind() function is indeed a standard library function in C. The rewind() function is used to set the file position indicator to the beginning of the file pointed to by the stream’s pointer. The stream represents the file, and by using opening and closing functions, the file’s structure is managed and supported efficiently. The rewind() function also can clear any error flags associated with the file.
Syntax:
Syntax of rewind() in C are:
void rewind(FILE *stream);
The syntax flow is in a way where the rewind function passes some parameters, which are as follows:
- FILE: represents the file that the indicator will point to.
- *stream: This is the file object of the stream class indicator that will point towards the file or end of the file, ignoring the irrelevant info in a file.
There is no return type for the rewind function, as it just sets the position of the file, which is not much for complex functionality.
How rewind() Function Works in C?
Like any other function rewind() function is also a function that is part of the standard library with a combination and composition of both the stream and file functions. Both functions are dependent on each other for the full fledge working of both in a way that both can mutually support each other in some or the other way. Rewind function() works in a way that has given programmers the ability to play around with the internal pointers to point towards the stream of data with the file. Let’s make the working of the rewind() function more simplified and easier by following these set of statements and functions :
- rewind() function in C considers the parameter as a file and the file object with an object pointing towards the stream.
- As part of the standard library, it gives the parameter the ability to position the file so that it must be placed at the beginning of the file towards which the file for the stream will get pointed to.
- In case the file pointed by the iterator comprises irrelevant information or if it contains some error or some other kind of not-so-related data or information, then it will not work as it should because the rewind() function has a unique ability to remove or clear this irrelevant or unwanted info till the end of the file. Its focus is to provide enough convenience to programmers in terms of input-output streams.
- The stream pointer present within the function emphasizes setting the file position at the beginning of the file.
- There is no return type once the rewind function is called for usage.
- There is a must header that will suffice and fulfill the working of the rewind function, which is
# include <stdio.h> - But the specific function works only with certain versions, which include ANSI/ISO 9899-1990.
- There is some other function that behaves almost like the rewind() function in C with very minute differences. That behavior can be used in accordance with the requirement of functions like fseek() function fsetpos() function.
- There is no time complexity which means for rewind() function, the time complexity comes out to be 0. Since this function is not much in the working state just makes the adjustment in terms of the pointer defined indicating for opening and closing of the file; thus, it does not involve much working with time and transformation.
- This function has a return type. On the other hand, rewind() function has no return type. it includes additional parameters like whence and offset, which work according to file and its stream.
- The fsetpos() function is used to set the position of the file object to a specific location within the file. It takes a file position object (fpos_t) as an argument, representing the desired position within the file. This function allows for precise positioning within the file based on the provided position. This function also has a return type that rewind() function never supports.
- All these functions are part of the standard library which needs <stdio.h> as a mandatory header.
Examples of rewind() in C
Following are the examples given below:
Example #1
This program demonstrates the file an_2.txt, which contains some information, and calling rewind() function makes the entire file read and open with the relevant information by setting it up before the beginning of the text, as shown in the output.
Code:
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fptr;
char c_h;
fptr=fopen("an_2.txt","r_ec_");
while((c_h=fgetc(fptr))!=EOF){
printf("%c",c_h);
}
rewind(fptr);
while((c_h=fgetc(fptr))!=EOF){
printf("%c",c_h);
}
fclose(fptr);
getch();
}
Output:
Example #2
This program demonstrates the comparison state of the fseek() function with the rewind() function. Apologies for the confusion. I misspoke in my previous response. The fseek() function does not override or add new content to the file. Instead, it allows you to reposition the file position indicator within the file. The existing file content remains unchanged, and subsequent read or write operations will occur from the specified position in the file.
Code:
#include <stdio.h>
#include <conio.h>
int main ()
{
FILE *fptr;
fptr = fopen("an_2.txt","w+");
fputs("Welcome_to_educba", fptr);
fseek( fptr, 7 , SEEK_SET );
fputs("Learning_to_code_Java", fptr);
fclose(fptr);
return(0);
}
Output:
Example #3
This program demonstrates the difference between the rewind function, fseek function, and fsetpos function, which have almost similar behavior except for a few minor changes and transformations. This behavior is similar to that of the rewind() function.
Code:
#include <stdio.h>
int main () {
FILE *fptr;
fpos_tposn;
fptr = fopen("an_2.txt","w+");
fgetpos(fptr, &posn);
fputs("Hello Everyone!", fptr);
fsetpos(fptr, &posn);
fputs("New_Content", fptr);
fclose(fptr);
return(0);
}
Output:
Conclusion
The standard library’s rewind() function is frequently used when working with input/output streams and file processing. It enables the moving of pointers or indications that refer to a particular collection of data inside a file.
Programmers get a sense of ease and flexibility by using rewind() function.
Recommended Articles
We hope that this EDUCBA information on “rewind() in C” was beneficial to you. You can view EDUCBA’s recommended articles for more information.