Javatpoint Logo
Javatpoint Logo

C fseek() function

The fseek() function is used to set the file pointer to the specified offset. It is used to write data into file at desired location.


There are 3 constants used in the fseek() function for whence: SEEK_SET, SEEK_CUR and SEEK_END.



This is sonoo jaiswal

Understanding the fseek() Parameters:

Let's understand the input parameters for the fseek() function is essential for complete use:

File*: The FILE *stream argument is a reference to the file's associated FILE structure. By utilizing the fopen() method, it is acquired.

Long int offset: The offset parameter specifies how many bytes will relocated to the whence parameter's location. It may be zero, positive, or negative.

Whence is an integer that indicates the reference place from which the offset is determined. Any one of the following three constants may be used:

SEEK_SET: It establishes the offset with respect to the file's start.

SEEK_CUR: It modifies the file pointer's offset to its current location.

SEEK_END: It establishes the offset with respect to the file's end.

Detailed Understanding of the fseek() Function

The three main tasks of the fseek() function are used to move the file pointer, write data to the specified place, and update the file position indication.

The file pointer is being moved: You can shift the file pointer to a specified area of the file using the fseek() method. You can change the reference point for the offset by altering the 'whence' parameter to one of the values SEEK_SET, SEEK_CUR, or SEEK_END.

Putting Data in the Right Place: Using operations like fprintf(), fputs(), fwrite(), etc., you may write data after shifting the file reference to the correct spot. The data will be written beginning at the file pointer's new location.

A file position indicator update: The fseek() method automatically updates the file position indication to reflect the changed location of the file pointer. By doing this, you may be confident that the file will always be operated on from the right angle.

Regular Use Cases of fseek()

There are several regular use cases of fseek() function. Some use cases are as follows:

Random Access: The fseek() function allows you random access to a file, enabling you to read or write data at any point you want inside the file. It is especially beneficial for huge files or databases when sequential data access is not the most effective method.

Modifying Existing Data: Without rewriting the whole file, fseek() lets you edit data at specified locations inside a file. You can replace or insert new data while keeping the present contents by shifting the file pointer to the appropriate spot.

Data Appending: You may relocate the file pointer to the end of the file and add additional data by using the fseek() function with the SEEK_END constant. It is quite helpful when working with log files or adding entries to an existing file.

Reading Specific Portions: You can read only the information you need by using the fseek() function to move to a specific location inside a file. Working with huge files or structured data can improve file reading procedures.

Metadata updating: In some circumstances, metadata may be present in places in files. You may access these metadata sections using fseek() and make the necessary changes. It is typical when working with file formats that include headers or other descriptive information.

Support for huge Files: The fseek() function is compatible with huge files when given a file offset of type long int. You may now work with files that are larger than what is allowed by standard file operations.

Considerations for Performance: Although fseek() function offers flexibility, frequent use of random-access operations can have an adverse effect on speed, particularly with big files. Consider the trade-off between convenience and performance when considering whether to use fseek() for file manipulation.

Here are several examples of how to use the C fseek() function and their accompanying results:

Example 1:

Consider the following text in a file called "data.txt":



Fourth line: Line 4

Example 2:

Let's say we have a binary file called "data.bin" that contains the following information:



File contents after writing: 1 2 3 10 5

Example 3:

Consider the following text in a file called "log.txt":



Log entry 1
Log entry 2
Log entry 3

Example 4:


File size: 28 bytes

The fseek() function in the C language allows programmers to set the file pointer to a given offset inside a file, making it an essential tool for file manipulation. Developers may perform various tasks, including random access, adding data, editing existing material, and reading particular file sections, skillfully utilizing the fseek() function.

For fseek() to operate properly, it is crucial to comprehend its syntax, which comprises the FILE pointer, offset, and whence arguments. The reference location from which the offset is computed is determined by the three variables SEEK_SET, SEEK_CUR, and SEEK_END, enabling precise control over the movement of the file pointer.

Programmers may manage big files, access data structures in binary files, and modify metadata sections with the help of fseek(). Additionally, the function has error-handling features that make it possible to identify and fix problems like looking outside of file boundaries or using closed files.

When using fseek(), it's crucial to consider the compromise between ease and speed. Performance can be impacted by excessive random-access operations, especially when dealing with big files. Therefore, it is essential to analyze how to optimize file handling and reduce pointless carefully seeks.

Developers may improve their ability to manipulate files by understanding the fseek() method, which will make their programs more effective, adaptable, and resilient. Programmers can easily handle sophisticated file operations due to fseek(), which enables file navigation, data modification, and content adding.

fseek() is a useful tool for manipulating file pointers and accessing data at precise points in the world of file management in C. By embracing and comprehending the potential of fseek(), developers may handle files with a new degree of precision and control, resulting in more complex and useful programs.

Next TopicC rewind()

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA