- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There are two methods:
1) From outside: use redirection property of command line interpreter, i.e.
2) From inside: redirect the output to a file using SetStdHandle API:
The trick with unit 6 used to work I think, but it's legitimate that it does not (anymore) in conjunction with write(*).
Jugoslav
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.txtas "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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks!
I got it. It's CloseHandle.
Sincerely,
Zhanghong Tang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page