Open Beliavsky opened 2 years ago
If you think the Fortran standard says unit 5 is stdin, 6 is stdout, and 0 is stderr -- Surprise! The standard says nothing about what units are used for pre-assigned files. Those are just often the units used by convention.
So how do you get the unit numbers in a portable way? Since f2003, you can use constants from the intrinsic module ISO_FORTRAN_ENV.
program demo_stderr
use,intrinsic :: iso_fortran_env, only : &
stdin=>input_unit, &
stdout=>output_unit, &
stderr=>error_unit
implicit none
character(len=256) :: line
read(stdin,'(a)') line
write(stdout,'(a)') line
write(stderr,*)'Error: program will now stop'
stop 4
end program demo_stderr
suggested by @urbanjost