fseek() function in C programming is a very important function in file handling which is used to position the current location.

What is fseek() function in C?

The file is a collection of data i. e file contains a large amount of data.
fseek function in C: – fseek function seeks (moves) cursor to the position in a file.
As the name implies fseek function seeks cursor to the position.
if you want to access data from anywhere in file you can move pointer cursor using fseek function and access.

syntax for fseek() function:-
fseek (FILE *fp ,int offset ,int whence);

In the above syntax, given the syntax for using fseek function which accepts 3 parameters which are as follows:
1. First is FILE *fp: – This parameter is the pointer to file.
2. int offset: – This parameter is the position of data which we want to find or accept.
3. int whence: – This parameter tells position where offset starts.

Whence in fseek() function

Different whence in fseek function which are as follows
SEEK_SET
SEEK_CUR
SEEK_END

whence in fseek function
whence in fseek function

fseek Example: –
Consider file with content:-
Technical seek

fseek() function in C
fseek() function in C

1. fseek (fp,0,SEEK_END);
In the above example, the fseek will point to end of file
=> NULL
2.fseek(fp,0,SEEK_SET);
In the above example, the fseek will start from the start
=> T

Why fseek() function?

In file if you want to access particular data present in file there are ways to do this:

1. One you can do is to iterate the loop till the end to get that data from the file.
But a problem with this option is,
– You have to iterate loop till the end.
– It requires a lot of time.
– Also, it increases the complexity.
– It requires more memory.

2. Another easy method is to use the fseek() function. Using fseek() gives you the flexibility to point to any position in the file.

Program for fseek() function in C

#include<stdio.h>
void main ()
{
FILE *fp;
char ch;
fp=fopen("technical.txt","r");. // Opening file in read mode
printf ("\n File seek");
fseek(fp,0,SEEK_END);. //seeking file
ch=fgetc(fp);
printf ("\n character at end of file = %c",ch);
fseek(fp,0,SEEK_SET);
ch=fgetc(fp);
printf ("\n character from start = %c",ch);
fseek(fp,3,SEEK_CUR);
ch=fgetc(fp);
printf("\n character from current position = %c",ch);
fclose(fp);
}

Output: –

fseek() function in C

Program description: –
In the above program, we seek to position to access character.

1. First, we open file in reading mode.
Let us assume technical.txt file exists at a location with content (content:- Define loop) and opened successfully by fopen function.
2. We use fseek function to seek in the file which accepts 3 parameters. First is file pointer, a pointer which points to file. Second is offset and third is the whence i.e where to start positioning in the file.
3. Using fseek function fseek(fp,0,SEEK_END) we position the cursor at end of file and access the 0 characters.
4. We get the character which pointer points using fgetc function.
5. Similarly, we use different whence to seek and access the character files.

Write A Comment