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

Redirecting Unit *

cjosefy
Beginner
722 Views
I'm having a problem with some code I'm porting from AIX to Linux. Here's the deal:
OPEN(UNIT=6,FILE='OUTPUT')
WRITE(*,*) "TEST"
On AIX this creates a file called OUTPUT and writes TEST to it.
On Linux this creates an empty file called OUPUT and writes TEST to the console.
I've tried setting FORT6, but it doesn't work. I can explicity write to unit 6 (i.e. write(6,*) ) and it works, but the code has about2000 WRITE(*,*) statements.
Is there a way to redirect the * unit number to a file?
Thanks.

Message Edited by cjosefy on 01-27-2005 07:39 AM

0 Kudos
11 Replies
TimP
Honored Contributor III
722 Views
On linux, unit=* normally is interpreted as stdout. The usual way to redirect is by the command line piping commands e.g.

./a.out > OUTPUT

20 years ago, it was common for unit=* to be the same as some numbered unit, but unit 6 was not the only common choice made by compilers.

Did you try
OPEN(unit=*,file='OUTPUT') ?
I don't think that is guaranteed, but it seems the most obvious way, if you want to build the file name into the program.
0 Kudos
Steven_L_Intel1
Employee
722 Views
You can't open unit *.

If you are going to open unit 6, do the writes to unit 6. As Tim says, unit * and unit 6 may well be different (and they are in Intel Fortran.)
0 Kudos
cjosefy
Beginner
722 Views
The Fortran Compiler for Linux User's Guide Vol. 1 (Pg. 166) says that WRITE(*,f) iolist is associated with the environment variable FORT6 and stdout. However, setting FORT6 has no effect on this behavior.
If UNIT 6 and * are different then the wording should be changed.

Message Edited by cjosefy on 01-27-2005 10:21 AM

0 Kudos
Steven_L_Intel1
Employee
722 Views
Thanks - I see the table you are referring to. I will have it looked into.
0 Kudos
cjosefy
Beginner
722 Views
Thanks a lot. For now, I think the temporary fix will be to call C's freopen() to redirect stdout. When you do that, WRITE(*,*) is redirected.
0 Kudos
Steven_L_Intel1
Employee
722 Views
I would expect that defining FOR_PRINT would do it with or without -vms. The reference to -vms in the table puzzles me....
0 Kudos
cjosefy
Beginner
722 Views
I compiled WITHOUT -vms and then set the FOR_PRINT environment variable. Now WRITE(*,*) outputs to the file specified in FOR_PRINT.
0 Kudos
Steven_L_Intel1
Employee
722 Views
That is what I would expect.
0 Kudos
jim_woodward
Beginner
722 Views
I have a similar problem that I don't think has been answered here. I have a c++ main that calls some legacy fortran code. The fortran code performs writes to unit 6. The c++ code needs to parse whatever is written to stdout without it going to a file. The c++ code sets up a pipe with stdout going in one end and the other end being read and parsed. This works for printf's and cout's. Fortran writes to either unit 6 or unit * just hang in the write. Is there a way to intercept unit 6 and * without writing to a file and without changing the legacy fortran code?

The pipe is set up as follows in the c++ code:

enum {READ, WRITE};
int my_pipe[2];
pipe(my_pipe);
dup2(my_pipe[WRITE], fileno(stdout));
.
.
num_bytes = read(my_pipe[READ], buffer, sizeof(buffer));
.
.
0 Kudos
Steven_L_Intel1
Employee
722 Views
It should "just work". Fortran will honor the redirection and write to the pipe. If you are finding that it doesn't, please submit an example to Premier Support.
0 Kudos
jim_woodward
Beginner
722 Views
Well, I wrote an example and, of course, it works ok. There must be something within the legacy code messing things up.

Thanks for the quick reply.
0 Kudos
Reply