×

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the file pointer to a specific location in the file so that subsequent read or write operations will occur at that location. In C, use the fseek() function  to move the file pointer to the desired location and point it  to a specific file location. Once the file pointer has been moved to a given offset, any operation including writing to or reading from the file can be performed as requested by the programmer.

Syntax

Here is the general syntax for using fseek:

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

The stream parameter is a pointer to the file stream you want to modify. The offset parameter is the number of bytes to move the file pointer, and the whence parameter specifies the starting point for the seek operation.

There are three possible values for whence:

  1. SEEK_SET: The file pointer is set to the absolute position specified by offset.
  2. SEEK_CUR: The file pointer is set to its current position plus the offset value.
  3. SEEK_END: The file pointer is set to the end of the file plus the offset value.

For example, to move the file pointer to the beginning of a file, you can use the following code:

fseek(stream, 0, SEEK_SET);

To move the file pointer to the end of the file, you can use the following code:

fseek(stream, 0, SEEK_END);

If the fseek function is successful, it returns 0. If an error occurs, it returns a non-zero value.

We have already seen the syntax of the fseek() function. It takes three arguments: the file pointer, the number of bytes or characters to move, and the position of the file pointer Therefore, fseek() is used in each of the three competitions along with other document functions to perform their respective tasks.

It basically acts as a C function that handles file operations The fopen() function is also built into C and  used to open any file and returns a file pointer. The 'numbytes' function returns the number of bytes from the beginning of the file. The desired read and write operations can be performed by locating and moving the file pointer using the C fseek() function.

The fseek function is typically used in conjunction with other functions from the C standard library, such as fread and fwrite, to perform read and write operations on a file. It allows you to move the file pointer to a specific location in the file so that you can read or write data at that location.One common use of seek is to move the file pointer to a specific location in the file before performing a read or write operation.

Example

Consider the following code:

#include <stdio.h>
int main() {
    // Open a file for reading
    FILE *fp = fopen("myfile.txt", "r");
    // Move the file pointer to the 10th byte of the file
fseek(fp, 10, SEEK_SET);
    // Read the next 10 bytes of the file into a buffer
    char buffer[11];
fread(buffer, 1, 10, fp);
    // Print the buffer
printf("%s\n", buffer);
    // Close the file
fclose(fp);
    return 0;
}

In this example, the file pointer is moved to the 10th byte of the file using fseek, and then the next 10 bytes of the file are read into a buffer using fread. The contents of the buffer are then printed on the screen.

You can also use fseek to write data to a specific location in a file. For example, the following code writes a string to the 10th byte of a file:

#include <stdio.h>
int main() {
    // Open a file for writing
    FILE *fp = fopen("myfile.txt", "w");
    // Move the file pointer to the 10th byte of the file
fseek(fp, 10, SEEK_SET);
    // Write a string to the file
fwrite("Hello, world!", 1, 14, fp);
    // Close the file
fclose(fp);
    return 0;
}

Conclusion

The C fseek() function  and its application in the C language are clearly explained in the previous discussion. Returns a value of zero if the fseek() activity is valid. Otherwise, it returns a non-zero value. Manipulating files is a big part of the C language, so there are many built-in functions such as fopen(), fclose(), getw(), and putw(). Before using a function, we need to know a lot about it to use it properly.


Related Topics

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.