If you had the foresight to write all your write
statements along the lines of
write(write_unit,*) stuff_to_write
it would be very simple, you could simply attach write_unit
to a file instead of stdout
. I guess you knew that already or you wouldn't be asking the question. If your write
statements are of the form
write(*,*)
or
print *
then you can probably redirect output with a little help from your compiler. Intel Fortran, for example, sends output directed to *
to the console, unless the program finds itself executing in an environment with the variable FOR_PRINT
set to the name of some file or other, in which case write(*,*)
will write to that file. I expect other compilers have similar capability.
If your code uses
write(6,*)
on the expectation that unit 6 points at stdout
you can open unit 6 on a file
open(unit = 6, file = 'stdout_redirect', status = 'new')
For any more you'd better tell us what your write statements look like and what compiler you're using.
EDIT
after OP's edit to question. This is more of a comment than an answer but I'm quite long-winded ...
Sadly, from the portability point of view, much of a Fortran program's interaction with the underlying computer system is not defined in the standards. Your question of where does output sent to unit *
go is a case in point; the latest (2008) standard says that it must go to the same unit as identified by the named constant output_unit
from the intrinsic module iso_fortran_env
. But it does not say that it goes to stdout
(there have been plenty of Fortran implementations on systems without a stdout
) and it does not specify how to redirect that output, or even if it is possible to do so. Redirection of output is a matter for the platform, not Fortran. As we've discovered Intel Fortran can use an environment variable to redirect output, apparently Cray Fortran can't.
The latest standard is entirely silent on the topic of file descriptors, I'm not sure it's a concept known to Fortran.
Using *
for i/o is essentially saying that it is up to the compiler / run-time system where input comes from and output goes to. If you want portability you have to take control and use specific unit identification. Personally I think you should take @george's advice in the comment below.