Assigning an I / O module number to a string variable
I have FORTRAN code that creates a .dll that is called from several different projects. In some cases, I want to write the output to a .txt file, and in other cases, the output is piped from the .dll, depending on the project that calls it. What I'm looking for is a way to handle the statement WRITE
the same anyway. That is, I'm looking for something like
SUBROUTINE MYPROGRAM(PROJECTFLAG,MYSTRING)
IF (PROJECTFLAG.EQ.1) THEN
OPEN(1,FILE='RESULTS.TXT')
ELSEIF (PROJECTFLAG.EQ.2) THEN
OPEN(1,MYSTRING) !THIS SYNTAX DOES NOT WORK
ENDIF
...
WRITE (1,*) OUTPUTDATA
END SUBROUTINE
As I already noted, the syntax I have above does not work. Is it possible to OPEN
use a variable for WRITE
, for example, or can you only OPEN
files?
I know I can move my if block and do something like
IF (PROJECTFLAG.EQ.1) THEN
WRITE(1,*) OUTPUTDATA
ELSEIF (PROJECTFLAG.EQ.2) THEN
WRITE(MYSTRING,*) OUTPUTDATA
ENDIF
but the code in the actual project is obviously much more complicated and my goal is to use the same operator WRITE
no matter where I want to write the data.
source to share
No, It is Immpossible. The compiler needs to know if the write statement is for internal I / O or external I / O. In the first case, you add an integer (device number) in the other case, when you specify a character string.
Also, you cannot open
create a character string.
Perhaps you could write a generic function and call it with either a device number or a string?
source to share