Software Archive
Read-only legacy content
17061 Discussions

Redirecting Standard I/O

Intel_C_Intel
Employee
996 Views
Hi,

I am porting a UNIX F77 program to the Windows platform. The program
is built as a multithreaded windows application (/subsystem=windows).
A C/C++ thread is used for the graphical user interface and a Fortran
thread does other tasks, e.g. solving equations. Therefore I need to
redirect the Fortran standard input and standard output (units 5 and 6) to
anonymous pipes. The pipes have been created in the C/C++ thread using
the CreatePipe() call. It is no problem to redirect the C/C++ stdin and
stdout to pipes , but I do not know how to redirect the Fortran units 5
and 6.

In other words: The Fortran read(5,...) statement has to read its
input from the pipe instead of reading from the non-existing
console (/subsystem=windows!). Also write(6,...) has to send its
output to a pipe insead of writing to the console.

Any help would be greatly appreciated.

Thanks,
Arne
0 Kudos
9 Replies
Steven_L_Intel1
Employee
996 Views
Arne,

The only thing I can think of is to open units 5 and 6 with USEROPEN routines that you write which return the appropriate read or write pipe handle. How do you do this in C/C++? If it was a named pipe, that would be easier.

Steve
0 Kudos
Intel_C_Intel
Employee
996 Views
I n C/C++ it is possible to duplicate a handle using the dup(h1) or dup2(h1,h2) system call. Using dup2() with h1 set to the reading end of the pipe and h2 set to 0 (0 is the stdin which Windows supplies to every program, regardless if written in C or Fortran) redirects stdin to the pipe closing the original channel. This also redirects the FORTRAN unit 5 to the pipe. Unfortunately closing the original stdin channel is not always a good idea. Some Windows versions kill the running process if its stdin gets closed. Therfore you normally use dup(0) to duplicate the handle to stdin and then redirect this new handle to the pipe. This trick works fine for C/C++, but Fortran programs crash as soon as they try to read from unit 5. Therefore I finally replaced the all Fortran read statements by a calls to a C function which does the job.

Arne
0 Kudos
Intel_C_Intel
Employee
996 Views
Hi, This message is for Steve L. I have ported a UNIX F77 to a console application with a dialog box. In here all my WRITE(6, and PRINT go on the console. But once in a while I need to print that output. Now in your reply to billsincl you suggested about using a.exe>b.output which works but I am not inclined to use the command window. So please write to me a small routine either in Fortran or in C/C++ which will redirect the console output while I am running the program from Windows(not from DOS); also I like to make this optional. As I am not familiar with mixed language programming please instruct me about that also.
Thanking you,
Dilip Paul
0 Kudos
Steven_L_Intel1
Employee
996 Views
Dilip,

Sorry, I can't write a program that does this for you. You can open unit 6 to a file and do your writes to unit 6, so output will go to a file.

You could also define a Windows shortcut to run your EXE with redirection, or specify the redirection in Developer Studio under Settings..Debug..Program arguments.

Steve
0 Kudos
Intel_C_Intel
Employee
996 Views
Hi, This message is for ArneSchultz. I have same problem like yours, although my WRITE(6,..) and PRINT are going on the console window I like to have the capabilty to save or not to save the wrtieups on the console window as an option on my dialog box. The solutions suggested by Steve L. is very limited as my program is being done for users who do not want to get entangled in complex methodolgy. I shall appreciate it very much if you could send me your createpipe() and dup() routines which I can modify to serve my purpose. Little explanation as to the making the exe will also be appreciated.

Dilip Paul
0 Kudos
Intel_C_Intel
Employee
996 Views
Hi, This message is for Steve L. I am planning to use the USEROPEN. In compaq Language manual it syas to look for examples in the Programmer's Guide- unfortunately there is nothing there. I shall appreciate it very much if you could give me some example with handles. This being(redirecting i/o) important, I suggest that Compaq fortran should include a chapter on this which should have both redirecting from command window and also from windows employing createpie() etc.

Thanks,
Dilip Paul
0 Kudos
Steven_L_Intel1
Employee
996 Views
Bring up the online documentation index and type USEROPEN - it brings you to a detailed description with an example.

Steve
0 Kudos
Intel_C_Intel
Employee
996 Views
This message is for Steve L. I got the USEROPEN and the sample program from the online documentation and was able to implement it successfully.
OPEN(UNIT=6,FILE='HFEAM.OUT',STATUS='NEW',USEROPEN=UOPEN)

STS = CreateFile( FILENAME, &......STS=UOPEN
My question is: how to close the handle STS so that I am able to edit the output file; the create file is within the function UOPEN. I should be able to close the handle in the main routine where OEN( ) is located. Presently I am using close(unit=6) in another routine which does the job. How may I be able to use ist=CloseHandle(STS) when the program ends? What is the advantage of UOPEN as opposed to the regular OPENFILE?

Thanks,
Dilip Paul
0 Kudos
Steven_L_Intel1
Employee
996 Views
Dilip,

If all you're doing is opening the file using CreateFile, then a simple CLOSE of the unit when desired should suffice. You can communicate the file handle to the main program with a module or COMMON variable, if desired.

Steve
0 Kudos
Reply