Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is fseek() in C?
3.
Syntax of Fseek() in C Function
4.
Parameters of fseek() in C
5.
Constraints of Fseek() in C Function
6.
Working of Fseek() Function in C
7.
Example of Fseek() Function in C
8.
Frequently Asked Questions
8.1.
What is the difference between fseek and ftell in C with syntax?
8.2.
What is the use of Lseek () fseek () ftell () and rewind () functions?
8.3.
What is the return value of fseek in C?
8.4.
What is the difference between rewind and fseek in C?
9.
Conclusion
Last Updated: Apr 20, 2024
Easy

C fseek() function

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

C is a popular basic programming language. The fseek() function is a standard inbuilt function of the C Programming Language. This function belongs to the <stdio.h> library. Its purpose is to set the file pointer at any specific position. After moving the file to a specific position, we can perform many operations like writing and reading according to our requirements. 

What is fseek() in C?

The fseek() function in C is used to set the file position indicator for a given file stream. It allows you to move the file pointer to a specified position within a file, enabling random access to different parts of the file.

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

Syntax of Fseek() in C Function

Syntax of Fseek() Function is:

int fseek(FILE *stream, long int offset, int pos);

 

The syntax is the path through which we pass parameters to the program of function. Following are the described parameters of the fseek() function.

  • Stream: A stream is a file pointer that indicates the file objects which determine the file stream.

 

  • Offset: Offset is the number of bytes or characters where the position indicator needs to be placed to set the position of the new file. The data type of offset is a long integer.

 

  • Pos: Pos indicates the position where the file offset needs to be added. In other words, it tells where the file pointer needs to be placed.

Parameters of fseek() in C

The fseek() function in C accepts three parameters, which control the positioning of the file pointer within a file stream. These parameters are:

  • stream: This parameter is a pointer to a FILE object representing the file stream whose file position indicator needs to be adjusted. The FILE object must have been previously opened using functions like fopen().
  • offset: The offset parameter specifies the number of bytes to move the file pointer. It can be a positive or negative integer value, indicating the relative distance to move the file pointer from a reference point.
  • whence: The whence parameter determines the reference point for the offset calculation. It specifies where the offset should be applied from. It can take one of the following values:
    • SEEK_SET: It sets the file pointer's position relative to the beginning of the file. The offset parameter specifies the number of bytes from the start of the file.
    • SEEK_CUR: It sets the file pointer's position relative to the current position. The offset parameter specifies the number of bytes to move forward (positive value) or backward (negative value) from the current position.
    • SEEK_END: It sets the file pointer's position relative to the end of the file. The offset parameter specifies the number of bytes from the end of the file. A negative offset moves the file pointer backward from the end of the file.

Constraints of Fseek() in C Function

The fseek() function works on three constraints. Following are the descriptions of those three constraints:

  • SEEK_CUR: It denotes the current location of the pointer of the file.

 

  • SEEK_END: As the name suggests, this command transfers the file's pointer to the end of the file.

 

  • SEEK_SET: As the name suggests, this command is used to transfer the file pointer to the starting of the file.

Working of Fseek() Function in C

As described above in the syntax, the fseek() function works on three parameters, where the first parameter is the file pointer, the second parameter is offset, i.e., the number of bytes/characters to be repositioned, and the third parameter informs about the position where the function should place the file pointer. So the fseek() function is used with all three parameters along with the other file functions to perform their defined tasks. 

Fseek() Function is generally a C function that is used in file handling and file operations. In the C programming language, there is an inbuilt function named fopen() that returns a file pointer as its output. This function is used for opening any file in C, and numbytes() is another inbuilt function of the C programming language, whose main purpose is to count the number of bytes of any original file. Using the fseek() function in C, we can perform operations like read and write and move the file to any specific location per our requirements.

Also Read -  Tribonacci Series

Example of Fseek() Function in C

Since the fseek() function puts the data in a specific position, in this example we will see how the fseek() function replaces the data of the file and prints it.

#include <stdio.h>  
#include<conio.h>

void main(){  
	FILE *tx;
	tx = fopen("newfile.txt","w+");
	fputs("Hello there, I am Gaurav Bashisht.", tx);
	fseek( tx, 12, SEEK_SET );  
	fputs("Welcome to Coding Ninjas.", tx);  
	fclose(tx);  
	int ch;
	
	printf(Resulting output of putting data after skipping 12 characters from beginning in a file are);

	while((ch=fgetc(tx)) != EOF){
 		putchar(ch);
 	}
	
	getch();
}  

 

Output:

Hello there, Welcome to Coding Ninjas.

 

Explanation:

In the above-given code, a file named 'newfile.txt' already exists in the computer. First of all, we include the stdio.h library, which is compulsory to use the fseek() function in C for any file-related I/O operations. Initially, the file is opened using the function fopen() in the read and write mode by the w+ process and returns the file pointer. Then after opening the file, we add a sentence using the fputs() function. Now by using the fseek() and fputs() function, we put another sentence in the file by skipping 12 positions from the beginning, and then we close the file using the fclose() function. This is the process through which we can place our data in a file to any position using the fseek() function.

Also see, Short int in C Programming

Frequently Asked Questions

What is the difference between fseek and ftell in C with syntax?

fseek(FILE *stream, long int offset, int whence): Sets the file position indicator for the specified file stream.
ftell(FILE *stream): Returns the current position of the file pointer within the specified file stream.

What is the use of Lseek () fseek () ftell () and rewind () functions?

Lseek(), fseek(), ftell(), and rewind() functions in C are used for file positioning and manipulation. They allow moving the file pointer, determining the current position, and resetting the file pointer to the beginning of the file.

What is the return value of fseek in C?

The fseek() function in C returns zero on success, indicating that the file pointer has been moved to the specified position. It returns a non-zero value (typically -1) on failure.

What is the difference between rewind and fseek in C?

rewind() sets the file pointer to the beginning of the file. fseek() moves the file pointer to a specified position within the file. rewind() is used to reset the file pointer to the start of the file quickly. fseek() is more versatile and can be used to move the file pointer to any position within the file.

Conclusion

This blog explains the fseek() function in C and its uses in C language. If the fseek() operation is successful, it returns a zero value; otherwise, it would produce a garbage value. As we all know, file handling is an essential aspect of the C language, and it has so many inbuilt functions like fopen(), fclose(), getw(), and putw(). Before using any of these functions,  it is essential to have good knowledge about the C language and its functions in advance to use it properly.

Also read,

 

Refer to our guided paths 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!

Previous article
C fputs() and fgets()
Next article
Rewind() Function in C
Live masterclass