fseek

Positions the file to the specified byte offset in the file.

Format

#include  <stdio.h>

int fseek  (FILE *file_ptr, long int offset, int
           direction);

Arguments

file_ptr
A file pointer.
offset
The offset, specified in bytes.
direction
An integer indicating whether the offset is to be measured forward from the beginning of the file (direction=SEEK_SET), forward from the current position (direction=SEEK_CUR), or backward from the end of the file (direction=SEEK_END).

Description

This function can position fixed-length record-access file with no carriage control or a stream-access file on any byte offset, but can position all other files only on record boundaries.

The available Standard I/O functions position a variable-length or VFC record file at its first byte, at the end-of-file, or on a record boundary. Therefore, the arguments given to fseek must specify any of the following:

See the fgetpos and fsetpos functions for a portable way to seek to arbitrary locations with these types of record files.


CAUTION
If, while accessing a stream file, you seek beyond the end-of-file and then write to the file, the fseek function creates a hole by filling the skipped bytes with zeros. In general, for record files, fseek should only be directed to an absolute position that was returned by a previous valid call to ftell, or to the beginning or end of a file. If a call to fseek does not satisfy these conditions, the results are unpredictable.

See also open, creat, dup, dup2, and lseek in this section.

Return Values
Indicates successful seeks. 
-1  Indicates improper seeks. 


Previous Page | Next Page | Table of Contents | Index