fseek

Date:

09-23-2013

NAME

fseek, ftell - Reposition a stream (C) or logical unit (FTN).

SYNOPSIS

C Synopsis

#include <stdio.h>

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

long ftell(FILE *stream)

Fortran Synopsis

integer unit
integer offset
integer whence

integer function fseek(unit, offset, whence)

integer funtion ftell(unit)

IMPLEMENTATION

Cray Linux Environment (CLE)

DESCRIPTION

fseek sets the file position. The new position, measured in bytes, is obtained by adding offset bytes to the position specified by whence. If whence is set to either 0, 1, or 2, the offset is relative to either the start of the file, the current position indicator, or end-of-file, respectively. On success, returns 0; otherwise, system error.

ftell returns the current value of the file position indicator for the file (or unit) which is an offset, in bytes, from the beginning of the file.

EXAMPLE

PROGRAM test_fseek
            INTEGER, PARAMETER :: SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2
            INTEGER :: fd, offset, ierr

            ierr   = 0
            offset = 5
            fd     = 10

            OPEN(UNIT=fd, FILE="fseek.test")
            ierr = FSEEK(fd, offset, SEEK_SET)  ! move to OFFSET
            print *, FTELL(fd), ierr

            ierr = FSEEK(fd, 0, SEEK_END)       ! move to end
            print *, FTELL(fd), ierr

            ierr =  FSEEK(fd, 0, SEEK_SET)      ! move to beginning
            print *, FTELL(fd), ierr

            CLOSE(UNIT=fd)
END PROGRAM

SEE ALSO

fseek(3)