Chapter Contents

Previous

Next
fseek

fseek



Reposition a File

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTIONS
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdio.h>

int fseek (FILE *f, long int offset, int type);


DESCRIPTION

fseek repositions the stream associated with the FILE object pointed to by f , as specified by the values of offset and type . The type value must be one of SEEK_SET , SEEK_CUR , or SEEK_END . (These constants are defined in <stdio.h> .) Each of the type values refers to a specific location in the file, as follows:

The interpretation of the offset value depends on the value of type and whether f identifies a text or binary stream.

For a binary stream, the offset value specifies the offset in characters of the new position from the location identified by type . Because of this ISO/ANSI requirement, fseek can be called for a binary stream only when the "rel" access method is used. Note that the offset value may be either positive or negative, but positioning before the start of the file is not supported. See File positioning with fseek and ftell for the details of positioning beyond the end of file.

When fseek is used with a text stream, two cases can be distinguished. If the value of offset is 0, the file is repositioned to the location identified by type . An offset value other than 0 is supported for a text stream only if type is SEEK_SET . In this case, offset must be a value previously returned by ftell for the same stream, and fseek restores the file position at the time of that call. Note that, in this case, the value in offset is an internal representation of the file position and cannot be interpreted as a relative character number.

After a call to fseek on a stream that permits both reading and writing, the next file operation may be input or output. (However, for an OS/390 PDS member, you can switch from reading to writing only at the start of the file.)


RETURN VALUE

If successful, fseek returns 0. If it fails, fseek returns a nonzero value and stores an appropriate value in errno . See the list of errno values in The errno Variable.


CAUTIONS

If output occurs after a call to fseek on a text stream, characters following the point of output may be erased from the file. This occurs when trunc=yes is in effect. Therefore, when trunc=yes is in effect, you are unable to return to a previous file position if output has been performed before that point.

See Opening Files for more information on the trunc amparm.


PORTABILITY

Be cautious when porting an fseek application because the implementation of fseek varies from system to system and library to library.


IMPLEMENTATION

Refer to File positioning with fgetpos and fsetpos for implementation details.


EXAMPLE

See the example for ftell.


RELATED FUNCTIONS

ksearch , kseek , lseek , fsetpos , ftell


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.