Standard error, standard input, and standard output can be referenced using named constants from the iso_fortran_env module. They can be renamed to “stderr”, “stdout”, and “stdin” if preferred.
use,intrinsic :: iso_fortran_env, only : stderr=>ERROR_UNIT
use,intrinsic :: iso_fortran_env, only : stdin=>INPUT_UNIT
use,intrinsic :: iso_fortran_env, only : stdout=>OUTPUT_UNIT
implicit none
character(len=255) :: string
write(stdout,*) 'written to stdout'
write(stderr,*) 'written to stderr'
read(stdin,'(a)') string
end
The unit numbers 5, 6, and 0 are conventionally the unit numbers associated with stdin, stdout, and stderr. They are a de-facto standard but are not specified in the Fortran standard at all, and the asterisk (“*”) is not guaranteed to be the same unit. The named constant names are defined by the standard and are able to be used in inquire(3) and open(3) statements with no ambiguity.
!-------------------------------------------------------------------------------
! Example: writing messages to standard error
! Standard: Fortran 2003
!
SUBROUTINE stderr(message)
! @(#) stderr writes a message to standard error using a standard f2003 method
USE ISO_FORTRAN_ENV, ONLY : ERROR_UNIT ! access computing environment
IMPLICIT NONE
CHARACTER(LEN=*),INTENT(IN) :: message
WRITE(ERROR_UNIT,'(a)')trim(message) ! write message to standard error
END SUBROUTINE stderr
!-------------------------------------------------------------------------------