Skip to content

Commit 384437f

Browse files
committed
init
1 parent a7b1619 commit 384437f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

file_or_console_output.f90

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
program file_or_console
2+
use, intrinsic:: iso_fortran_env, only: stdout=>output_unit
3+
4+
implicit none
5+
6+
character(:), allocatable :: filename
7+
character(256) :: buf
8+
integer :: i, u
9+
10+
call get_command_argument(1,buf,status=i)
11+
if (i==0) then
12+
filename = trim(buf) ! Fortran 2003 auto-allocate
13+
print *, 'writing to ',filename
14+
open(newunit=u, file=filename, form='formatted')
15+
else
16+
u = stdout
17+
endif
18+
19+
i = 3 ! test data
20+
21+
write(u,*) i, i**2, i**3
22+
23+
if (u /= stdout) close(u) ! closing stdout can disable text console output
24+
25+
print *,'goodbye'
26+
27+
! end program implies closing all file units, but here we close in case you'd use in subprogram (procedure), where the file reference would persist.
28+
end program

0 commit comments

Comments
 (0)