Home » C programs » stdio.h header file functions

rewind() function in C language with Example

Here, we are going to learn about the rewind() function of library function stdio.h in C language with its syntax, example.
Submitted by Souvik Saha, on February 28, 2019

rewind() function in C

The rewind() function is defined in the <stdio.h> header file.

Prototype:

    void rewind(FILE *filename);

Parameters: FILE *filename

Return type: void

Use of function:

When we are dealing with files then sometimes we need to start of the specified files. In file handling, we use rewind() function to move the file position indicator to start of the specified file stream. The prototype of the function rewind() is void rewind(FILE *filename);

Here, filename is the name of the file where the file indicator starts. By the function, end-of-file and error flag is cleared.

rewind() example in C

#include  <stdio.h>
#include <stdlib.h>

int main(){
	//Initialize the file pointer
	FILE *f;
	char ch[100];

	//Create the file for write operation
	f=fopen("includehelp.txt","w");
	printf("Enter five strings\n");
	for(int i=0;i<4;i++){
		//take the strings from the users
		scanf("%[^\n]",&ch);
		//write back to the file
		fputs(ch,f);
		//every time take a new line for the new entry string 
		//except for last entry.Otherwise print the last line twice
		fputs("\n",f);

		//clear the stdin stream buffer
		fflush(stdin);
	}
	//take the strings from the users
	scanf("%[^\n]",&ch);
	fputs(ch,f);
	//close the file after write operation is over
	fclose(f);

	//open a file
	f=fopen("includehelp.txt","r");
	printf("\n...............print the strings..............\n");
	while(!feof(f)){
		//takes the first 100 character in the character array 
		fgets(ch,100,f);
		//and print the strings
		printf("%s",ch);
	}
	rewind(f);
	printf("\n...............print the strings again..............\n");
	while(!feof(f)){
		fgets(ch,100,f);
		printf("%s",ch);
	}

	//close the file
	fclose(f);

	return 0;
}

Output

rewind() example in C language






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.