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();
}
You can also try this code with Online C Compiler
Run Code
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!