0% found this document useful (0 votes)
32 views1 page

Fseek in C

The fseek() function in C is used to move the file pointer to a desired position in the file. It takes three arguments - a file pointer, an offset specifying the number of bytes/characters to move from a given position, and the position itself which can be SEEK_SET, SEEK_CUR, or SEEK_END to move relative to the start, current, or end of the file. Examples demonstrate using negative offsets to move the pointer backwards within a file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views1 page

Fseek in C

The fseek() function in C is used to move the file pointer to a desired position in the file. It takes three arguments - a file pointer, an offset specifying the number of bytes/characters to move from a given position, and the position itself which can be SEEK_SET, SEEK_CUR, or SEEK_END to move relative to the start, current, or end of the file. Examples demonstrate using negative offsets to move the pointer backwards within a file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

What is fseek() in C?

The fseek() function is used to move the cursor in the file to the desired position.
Syntax of fseek() function

int fseek( FILE *fp, long offset, int pos );

The fseek() function takes three arguments,


 fp: file pointer
 Offset: It is defined in ‘long integer’ data type and is used to specify the offset in terms of the number of
bytes or characters where the position indicator needs to be placed to define the new file position.
 pos: It defines the point where the file offset needs to be added. In other terms, it defines the position where
the file pointer needs to be moved.

Note: An offset into a file is simply the character location within that file.

There are three positions from where offset can move.

Constant Name Constant Value Description


SEEK_SET 0 The beginning of file
SEEK_CUR 1 The current position in file
SEEK_END 2 The end of file

1. fseek(fp, 0, SEEK_END);
Offset = 0, new file position is the beginning of the file
Position = SEEK_END, file pointer is moved to the end of the file.

2. “Welcome home”
fseek(fp,-4,2);
fprintf(fp,"removed");
offset = -4, new file position is “h” in “welcome home”
position = 2, means its SEEK_END, so the file pointer is moved to the end of the file at last.
OUTPUT: Welcome removed

3. “Welcome removed”
int n = 5;
fseek(fp,-8,2);
fprintf(fp,"%d",n);
offset = -8, new file position is “space” between Welcome and removed.
Pos = 2, means after manipulations in file, pointer moves to end of file
OUTPUT: Welcome5removed

NOTE: Negative values in offset means, the file pointer positions can be moved backwards also.
W e l c o m e h o m e
Bytes 0 1 2 3 4 5 6 7 8 9 10 11
-4 -3 -2 -1 From, fp = 0, offset = -4

You might also like