Beliavsky / FortranTip

Short instructional Fortran codes associated with Twitter @FortranTip
https://zmoon.github.io/FortranTipBrowser/
The Unlicense
64 stars 14 forks source link

writing to stderr #1

Open Beliavsky opened 2 years ago

Beliavsky commented 2 years ago

suggested by @urbanjost

urbanjost commented 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