Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

write(*,*) and write(6,*)

Zhanghong_T_
Novice
1,883 Views
Hi all,
Now I wantto prevent some fortran code outputting characters to the screen. As far as I know, the default unit number is 6 when print to the screen. so I add the following code before calling the code:
open(6,file='screen.txt')
As a test, I print the characters to the screen by 'write(*,*)' and 'write(6,*)' respectively. However, only the latter method can output the characters to the file. Can I away intercept any characters outputted to the screen and output them into a given file?
Thanks,
Zhanghong Tang
0 Kudos
6 Replies
Jugoslav_Dujic
Valued Contributor II
1,883 Views
There are two methods:

1) From outside: use redirection property of command line interpreter, i.e.
yourprog > whatever.txt
. If you want to run from VS, specify
cmd /c yourprog > whatever.txt
as "Executable for debug session".

2) From inside: redirect the output to a file using SetStdHandle API:
hFile = CreateFile("Whatever.txt"C, GENERIC_WRITE, &
FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0)
ret = SetStdHandle(STD_OUTPUT_HANDLE, hFile)


The trick with unit 6 used to work I think, but it's legitimate that it does not (anymore) in conjunction with write(*).

Jugoslav
0 Kudos
Zhanghong_T_
Novice
1,883 Views
Hi Jugoslav,
Thank you very much! I just like your second method. Another question: how can I close the file as the close in fortran?
Thanks,
Zhanghong Tang
0 Kudos
Zhanghong_T_
Novice
1,883 Views
Thanks!
I got it. It's CloseHandle.
Sincerely,
Zhanghong Tang
0 Kudos
Steven_L_Intel1
Employee
1,883 Views
In our current implementation, WRITE(*) and WRITE(6) are distinct units. (Unit * is encoded as a negative unit number.) Some time in the future, we'll very likely have to change that so that they are the same, as Fortran 2003 would appear to require it. It's funny, because we used to have them the same and split them because we thought Fortran 90 required them to be separate. C'est la vie....
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,883 Views
Some time in the future, we'll very likely have to change that so that they are the same, as Fortran 2003 would appear to require it.

Are you positive about that Steve? My (superfluous) reading is that all you need to do is to provide a module called, um, ISO_some_constants which will contain a constant describing the default input, output and error unit numbers (similar to STD_INPUT_HANDLE in Windows API).

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
1,883 Views
The problem is that we use negative unit numbers, which aren't allowed to be used directly in a Fortran program. So we have to pick some non-negative unit, and 5/6 is the most appropriate choice.
0 Kudos
Reply