Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

fseek() Function in C

Last Updated on January 16, 2024 by Ankit Kochar

The fseek() function in C is a powerful tool for file manipulation, enabling precise control over the positioning of the file pointer within a file. This function plays a crucial role in handling file operations, allowing developers to navigate through files, read or write data at specific locations, and manage file content efficiently. Understanding the nuances of fseek() is essential for anyone working with file I/O in the C programming language. In this article, we will delve into the intricacies of the fseek() function, exploring its syntax, applications, and best practices for effective file handling.

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 fundamental aspect of file manipulation in C, offering a versatile mechanism for positioning the file pointer within a file. By comprehending its usage, programmers can optimize file operations, enhance efficiency, and implement advanced file-handling functionalities. Whether navigating through large datasets or managing complex file structures, the fseek() function empowers developers to exercise precise control over file pointers, ultimately contributing to the creation of robust and efficient C programs.

Frequently Asked Questions about fseek() function in C

Here are some of the frequently asked questions about fseek in c.

1. What is the purpose of the fseek() function in C?
The fseek() function is used to set the file position indicator at a specified location within a file, enabling precise control over file pointer movements.

2. What is the syntax of the fseek() function?
The syntax is: int fseek(FILE *stream, long int offset, int whence);

3. How does the offset parameter in fseek() work?
The offset parameter determines the number of bytes the file pointer should be moved. It can be positive, negative, or zero, depending on the desired direction of movement.

4. Explain the whence parameter in the fseek() function.
The whence parameter specifies the reference point for the offset. It can take values like SEEK_SET, SEEK_CUR, or SEEK_END, determining whether the offset is calculated from the beginning, current position, or end of the file, respectively.

5. What is the return type of the fseek() function?
The fseek() function returns zero on success and a non-zero value on failure.

6. How can fseek() be used to move the file pointer to the beginning of a file?
To move the file pointer to the beginning, use fseek(file_ptr, 0, SEEK_SET);.

7. In what scenarios is the fseek() function particularly useful?
fseek() is especially useful when working with random access files, where direct access to specific locations within the file is required.

8. What happens if fseek() encounters an error?
If an error occurs during the fseek() operation, it returns a non-zero value. You can use perror() or strerror() to obtain an error message.

9. Can fseek() be used with text files?
Yes, fseek() is applicable to both binary and text files. It allows precise positioning of the file pointer regardless of the file type.

10. Are there any limitations or considerations when using fseek()?
While fseek() is a powerful function, developers should be cautious about potential issues with large files or files opened in modes that do not support seeking operations. Always check return values for error handling to ensure robust file manipulation.

Leave a Reply

Your email address will not be published. Required fields are marked *