fseek

Changes the read/write position of a stream

Local Servers:nonblocking
Remote Servers:nonblocking
Classification:ANSI
Platform:NLM
Service:Stream I/O

Syntax

  #include <stdio.h>  
   
  int fseek  (  
     FILE       *fp,   
     long int    offset,   
     int         where);
  

Parameters

fp
(IN) Points to the file.
offset
(IN) Specifies the file position to seek.
where
(IN) Specifies the relative file position.

Return Values

The fseek function returns a value of 0 if successful, nonzero otherwise. If an error occurs, errno is set.

Remarks

The fseek function changes the read/write position of the file specified by fp. This position defines the character to be read or written on the next I/O operation on the file. The argument fp is a file pointer returned by fopen or freopen. The argument offset is the position to seek, relative to one of three positions specified by the argument where. Allowable values for the where parameter are:

SEEK_SET

Relative to beginning of file; the offset must be positive.

SEEK_CUR

Relative to the current position in the file.

SEEK_END

Relative to the end of the file.

The fseek function clears the end-of-file indicator and undoes any effects of the ungetc function on the same file.

Call ftell to obtain the current position in the file before changing it. Restore the position by using the value returned by ftell in a subsequent call to fseek with the where parameter set to SEEK_SET.

See Also

fgetpos, fopen, fsetpos, ftell, lseek

fseek Example

You can determine the size of a file by means of the following example, which saves and restores the current position of the file.

  #include <stdio.h> 
    
  long int   filesize (FILE *fp)  
  {  
     long int   save_pos, size_of_file;  
     save_pos = ftell (fp);  
     fseek (fp, 0L, SEEK_END);  
     size_of_file = ftell (fp);  
     fseek (fp, save_pos, SEEK_SET);  
     return (size_of_file);  
  }