Home » C programs » stdio.h header file functions

fseek() function in C language with Example

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

fseek() function in C

Prototype:

    int feek(FILE *stream, long int offset, int origin);

Parameters:

    FILE *stream, long int offset, int origin

Return type: int

Use of function:

The fseek() function is used to set the file indicator pointer to the associate with the file stream according to the value of offset and starting point of the file. The prototype of the function fseek() is:

    int feek(FILE *stream, long int offset, int origin);

Here, the offset is number of bytes from the origin.

There are three macros in fseek() function:

  1. SEEK_SET: Seek from start of file
  2. SEEK_CUR: Seek from current location
  3. SEEK_END: Seek from end of file

fseek() example in C

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

int main(){
	//Initialize the file pointer
	FILE *f;
	//Take a array of characters 
	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 position before print a strings S..............\n");

	fseek(f,0,SEEK_SET);
	printf("the file indicator position is - %d\n",ftell(f));
	printf("\n...............print the position after print a strings ..............\n");
	fgets(ch,100,f);
	printf("%s",ch);

	fseek(f,0,SEEK_CUR);
	//print the current location
	printf("the file indicator position is - %d\n",ftell(f));    
	printf("\n...............print the position after print all the strings ..............\n");

	fseek(f,0,SEEK_END);
	printf("the file indicator position is - %d\n",ftell(f)); 
	
	//close the file
	fclose(f);
	
	return 0;
}

Output

fseek example in c





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.