Last Updated on March 30, 2023 by Prepbytes
One of the important features of any programming language is file handling. This feature allows the program to interact with files stored on the system. In C programming language we achieve file handling through the use of standard input/ output functions like fopen, fclose, fread, fwrite, etc. But these functions allow us to read and write the data either from the front or to the end but sometimes we need to read and write the data from any specific location, in these types of scenarios fseek in c comes into play. While moving further in this article we will learn all about fseek in c in detail, its usage in file handling, and much more.
fseek() in C
The fseek in c is a built-in function that allows the programmer or developer to move the file pointer to a specific location within a file. The function is declared in the stdio.h header file in the c programming language.
Syntax of fseek() in C
The syntax of Fseek in C is given below:
fseek(FILE *filePtr, long offset, int origin);
Parameters of fseek() in C
There are three parameters of fseek function in c.
- filePtr: IT is a file pointer that points to the file object of the stream which we need to modify.
- Offset: The data type of the offset is a long integer. It is the number of characters or bytes, where the position of the file pointer needs to be shifted relative to the current position to determine the new position.
-
Origin: It shows the contemporary position of the file pointer from which the specified offset value will be added, in order to move the file pointer’s position using the fseek() function. This function determines the current position of the FILE pointer from which the offset needs to be added.
The origin can have the following three values:
- SEEK_SET: It denotes the beginning or starting of the file.
- SEEK_CUR: It denotes the current position of the file pointer.
- SEEK_END: It denotes the end of the file.
Return type of fseek() in C
If the fseek in c is successfully executed it will return 0. It means only on successful execution of fseek it will return 0 otherwise it will return a non-zero value or error.
The fseek in c is mainly useful when we are dealing with a large file or working on a specific location within a file. It can be used to skip all the unwanted data and write or read data to specific locations.
Examples of fseek() in C
In this section, we will see various examples of fseek in c with proper code implementation.
Example 1 of fseek in C: Moving the File pointer
In this example we will see how to move the file pointer using fseek in c.
Code Implementation
#include <stdio.h> int main() { FILE *filePointer; filePointer = fopen("sample.txt", "r"); printf("The location of the current pointer is %d bytes from the start of the file\n", ftell(filePointer)); fseek(filePointer, 6, SEEK_SET); printf("The location of the current pointer is %d bytes from the start of the file\n", ftell(filePointer)); fclose(filePointer); return 0; }
Output
The location of the current pointer is 0 bytes from the start of the file
The location of the current pointer is 6 bytes from the start of the file
Content of the Input File
Hello Everyone
Hello Everyone, Good Morning
Hello Everyone, Good Afternoon
Explanation of the above example
In the above example we have moved the file pointer 6 bits from the current position using fseek in c. First, we opened the file using fopen after that using fseek we changed the position. ftell will tell us the position of the file pointer.
Example 2 of fseek in C: Calculating the size of the File
In this example we will be calculating the size of the non-binary file using fseek in c.
Code Implementation
#include <stdio.h> int main() { FILE *filePointer; filePointer = fopen("sample.txt", "r"); fseek(filePointer, 0, SEEK_END); printf("The size of the specified file is %d bytes\n", ftell(filePointer)); fclose(filePointer); return 0; }
Output
The size of the specified file is 77 bytes
Content of the Input File
Hello Everyone
Hello Everyone, Good Morning
Hello Everyone, Good Afternoon
Explanation of the above example
In the above example we will open the file using fopen after that we will set the current position of the file pointer. Now ftell will tell us the current position of the file pointer which will tell us the size of the input file.
Applications of fseek() in C
The fseek in c has many applications some of which are mentioned below:
- It is used to read specific records from a file, when working with a large file it is very beneficial to read only the specific data from the file.
- We can also write to specific locations in the file so rather than deleting the whole file and making a new we can directly edit the already existing file.
- We can use it to search for specific records in the database. This can be used to update data in the database.
- It is used in managing large datasets. It is used to move the file pointer to the appropriate location in the chunk of data.
Conclusion
In conclusion, the fseek() function is a powerful and versatile function that is essential for file-handling operations in the C programming language. It allows us to move the file pointer to a specific location within a file, skip over unwanted data, read or write data to specific locations, and reposition the file pointer after a previous read or write operation. The fseek() function is particularly useful when working with large files or when seeking to a specific location within a file. By using the fseek() function, we can perform more complex file-handling operations in the C programming language and make our programs more efficient and versatile.
Frequently Asked Questions
Here are some of the frequently asked questions about fseek in c.
1. What is the use of the fseek() function in C?
The fseek() function is used to move the file pointer to a specific location within a file.
2. How to skip over unwanted data in a file using the fseek() function in C?
To skip over unwanted data in a file using the fseek() function in C, we need to set the origin to SEEK_SET and the offset to the number of bytes we want to skip.
3. What happens if fseek() function fails in C?
If the fseek() function fails in C, it returns a non-zero value.
4. How to check if fseek() function fails in C?
To check if fseek() function fails in C, we need to check the return value of fseek() function. If it is non-zero, then fseek() function has failed.