Next: FSTAT, Previous: FREE, Up: Intrinsic Procedures

### 8.92 `FSEEK` — Low level file positioning subroutine

Description:
  Moves UNIT to the specified OFFSET. If WHENCE is set to 0, the OFFSET is taken as an absolute value `SEEK_SET`, if set to 1, OFFSET is taken to be relative to the current position `SEEK_CUR`, and if set to 2 relative to the end of the file `SEEK_END`. On error, STATUS is set to a nonzero value. If STATUS the seek fails silently.

  This intrinsic routine is not fully backwards compatible with `g77`. In `g77`, the `FSEEK` takes a statement label instead of a STATUS variable. If FSEEK is used in old code, change

                  CALL FSEEK(UNIT, OFFSET, WHENCE, *label)

  to

                  INTEGER :: status
                  CALL FSEEK(UNIT, OFFSET, WHENCE, status)
                  IF (status /= 0) GOTO label

  Please note that GNU Fortran provides the Fortran 2003 Stream facility. Programmers should consider the use of new stream IO feature in new code for future portability. See also Fortran 2003 status.

Standard:
  GNU extension
Class:
  Subroutine
Syntax:
  `CALL FSEEK(UNIT, OFFSET, WHENCE[, STATUS])`
Arguments:

  UNIT | Shall be a scalar of type `INTEGER`.
  OFFSET | Shall be a scalar of type `INTEGER`.
  WHENCE | Shall be a scalar of type `INTEGER`. Its value shall be either 0, 1 or 2.
  STATUS | (Optional) shall be a scalar of type `INTEGER(4)`.

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")
                  CALL FSEEK(fd, offset, SEEK_SET, ierr)  ! move to OFFSET
                  print *, FTELL(fd), ierr

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

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

                  CLOSE(UNIT=fd)
                END PROGRAM

See also:
  FTELL