C programming fseek stdio.h


In C programming the <stdio.h> fseek function can set the file indicator position to the new specified position as indicated by the programmer.The declaration of the function is given below.

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

Parameters:
stream -The pointer to file which should be processed.

offset -The reference position from which the file indicator is to be moved.This value may be the beginning of the file,the end of the file or the previous position.The elligible offset macro with it’s definition is shown below.

ValueMacro nameMeaning
0SEEK_SETThe beginning of the file
1SEEK_CURThe current position
2SEEK_ENDThe end of the file

Besides the above value the ‘offset’ value can be the value returned by the ‘ftell‘ function call.

whence -The position to which the file indicator should point from the ‘offset’ position.

Return type
int -Returns nonzero only for a request that cannot be satisfied.If a read or write error occurs,the error indicator for the stream is set and ‘fseek‘ fails.

The program below moves the file indicator to the 11th position.After ‘fseek’ is called the reading of characters from from the 11th position.To understand the program consider that a file “text.txt” with the content “123456789 10 11 12 13 14 15 16 17 18 19 20” exists.

Code example

FILE *fp=fopen( “text.txt” , “r+” );

int whence=0 , /*’0′ here means the beginning of the file,you can also use SEEK_SET,they are equivalent*/
offset=10 ,
val , ret ;

if (fp != NULL)
{
ret=fseek( fp , offset , whence); //set the file indicator position to the 11th character

printf(“ret=%d\n” , ret);

//access the 11th and remaining characters
while ( (val = fgetc(fp)) != EOF)
{
printf(“%c” , val);
}
}
else
 printf(“File cannot be opened”);

fclose( fp );

Output,

ret=0
10 11 12 13 14 15 16 17 18 19 20

As you can see reading starts from the 11th character.





Some points to note about fseek

i)Setting offset to negative value or past the end-of-file

We can set the ‘offset’ value to a negative value to access the position backward,but the position indicator cannot be moved backward past the beginning of the file.A code example is given below.For this program also consider the ‘text.txt’ file.

Code example

FILE *fp=fopen( “text.txt” , “r+” );

int offset=-5 ,
val , ret , count=0 ;

if (fp != NULL)
{
//Access the first 12 characters
while ( count< 12)
{
val=fgetc( fp );
printf(“%c” , val);
count++;
}

/** fseek( fp , offset , 0 ); //error! cannot move the file indicator before the beginning of the file**/

ret=fseek( fp , offset , SEEK_CUR); //Move 5 position backward from the current position(the 13th position)

printf(“ret=%d\n” , ret);

//access the 8th and remaining characters
while ( (val = fgetc(fp)) != EOF)
{
printf(“%c” , val);
}
}
else
 printf(“File cannot be opened”);

fclose( fp );

Output,

123456789 10
ret=0
89 10 11 12 13 14 15 16 17 18 19 20


ii)Binary file

A binary stream need not meaningfully support ‘fseek’ calls with a ‘whence’ value of SEEK_END.


iii)After fseek call

A successful call to the ‘fseek’ undoes any effects of the ‘ungetc’ function on the stream,clears the end of-file indicator for the stream,and then establishes the new position.After a successful ‘fseek’ call,the next operation on a stream may be either an input or output.


Related links

->C ungetc stdio.h

->C rewind stdio.h

->C fopen stdio.h

->C fclose stdio.h