×

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

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.