Redirecting output to a scratch file 
Author Message
 Redirecting output to a scratch file

Hi all,

Quote:
>From a fortran program I need to run an external program and read its

output. I would like to redirect this output to a fortran scratch file
so that it is deleted when closed and there are no name conflicts. Is
this, or something similar, possible in a more or less portable way in
fortran (although I realize the "CALL SYSTEM()" I'm using is not fully
portable)?

Thanks



Mon, 21 Sep 2009 15:23:46 GMT  
 Redirecting output to a scratch file

Quote:

> From a fortran program I need to run an external program and read its
> output. I would like to redirect this output to a fortran scratch file
> so that it is deleted when closed and there are no name conflicts. Is
> this, or something similar, possible in a more or less portable way in
> fortran (although I realize the "CALL SYSTEM()" I'm using is not fully
> portable)?

I surmise that by "scratch file" you refer to the status value in the
open statement and then your goal would not be possible, there seems to
be no way to get to the file name of the scratch file.

You could write your own little scratch file handler, a module perhaps
which has some kind of initialisation and deinitialisation routines that
take care of open scratch file units.

Sebastian



Mon, 21 Sep 2009 15:34:29 GMT  
 Redirecting output to a scratch file

Quote:
> I surmise that by "scratch file" you refer to the status value in the
> open statement and then your goal would not be possible, there seems to
> be no way to get to the file name of the scratch file.

Yes, that's what I meant. If it's not possible I will probably just
use some arbitrary name for the file and delete it when closing...
Would one really need to know the filename for a scratch file?
Couldn't it be done by (temporarily) attaching the standard output to
a file descriptor (an already open scratch file)? Just wishful
thinking, I guess.


Mon, 21 Sep 2009 15:50:54 GMT  
 Redirecting output to a scratch file

Quote:

> > I surmise that by "scratch file" you refer to the status value in the
> > open statement and then your goal would not be possible, there seems to
> > be no way to get to the file name of the scratch file.

> Yes, that's what I meant. If it's not possible I will probably just
> use some arbitrary name for the file and delete it when closing...
> Would one really need to know the filename for a scratch file?
> Couldn't it be done by (temporarily) attaching the standard output to
> a file descriptor (an already open scratch file)? Just wishful
> thinking, I guess.

You are looking for:

open( 10, status = 'scratch' )

... do something with the file

close( 10 ) ! No trace remains

Scratch files are nameless files (or at least files for which
you need not and can not give a name) that are deleted automatically
when you close them (*)

Mind you: not all compilers support this as neatly as I am
suggesting now (I noticed the other day g77 will leave a file
around)

Regards,

Arjen

(*) you can close and delete ordinary files too
with the close statement, but then you need to specify:
close( 10, status = 'delete')



Mon, 21 Sep 2009 18:17:30 GMT  
 Redirecting output to a scratch file

Quote:
> You are looking for:

> open( 10, status = 'scratch' )

> ... do something with the file

> close( 10 ) ! No trace remains

Yes, I know, but this unnamed file should get the output of a CALL
SYSTEM() statement (which would usually go to the standard output...


Mon, 21 Sep 2009 18:57:19 GMT  
 Redirecting output to a scratch file

Quote:

> > I surmise that by "scratch file" you refer to the status value in the
> > open statement and then your goal would not be possible, there seems to
> > be no way to get to the file name of the scratch file.

> Yes, that's what I meant. If it's not possible I will probably just
> use some arbitrary name for the file and delete it when closing...
> Would one really need to know the filename for a scratch file?
> Couldn't it be done by (temporarily) attaching the standard output to
> a file descriptor (an already open scratch file)? Just wishful
> thinking, I guess.

If you're under Linux, a named pipe might do what you want:

call system('mkfifo output.fifo')
call system('./program > output.fifo')
open(10,file='output.fifo',action='read')
! ... read
close(10)

there's also a unix C library popen() function that does the same
thing (with anonymous, memory pipes). Some compilers (e.g. Fortran)
allow you to call it and convert the descriptor to Fortran unit number.



Mon, 21 Sep 2009 19:03:41 GMT  
 Redirecting output to a scratch file

Quote:

> > I surmise that by "scratch file" you refer to the status value in the
> > open statement and then your goal would not be possible, there seems to
> > be no way to get to the file name of the scratch file.

> Yes, that's what I meant. If it's not possible I will probably just
> use some arbitrary name for the file and delete it when closing...
> Would one really need to know the filename for a scratch file?
> Couldn't it be done by (temporarily) attaching the standard output to
> a file descriptor (an already open scratch file)? Just wishful
> thinking, I guess.

You could just check to see if your named scratch file, say '$$$.tmp',
already exists, then quit early if it does.

-- elliot



Mon, 21 Sep 2009 19:05:12 GMT  
 Redirecting output to a scratch file

Quote:

> > You are looking for:

> > open( 10, status = 'scratch' )

> > ... do something with the file

> > close( 10 ) ! No trace remains

> Yes, I know, but this unnamed file should get the output of a CALL
> SYSTEM() statement (which would usually go to the standard output...

Hm, misinterpreted your question, but that does leave open this
option:

call system( 'myprogram > hidden.out' )
open( 10, file = 'hidden.out' )
... do something with the file
close( 10, status = 'delete' )

where "hidden.out" can be a file name that you select yourself,
for instance:

do i = 1,1000
   write( unique_name, '(a,i4.4)' ) 'hidden.', i
   inquire( file = unique_name, exists = exist )
   if ( .not. exist ) exit
enddo

Apart from system() that ought to be pretty portable.

Regards,

Arjen



Mon, 21 Sep 2009 19:23:01 GMT  
 Redirecting output to a scratch file


Quote:


>> > I surmise that by "scratch file" you refer to the status value in the
>> > open statement and then your goal would not be possible, there seems to
>> > be no way to get to the file name of the scratch file.
>> Yes, that's what I meant. If it's not possible I will probably just
>> use some arbitrary name for the file and delete it when closing...
>> Would one really need to know the filename for a scratch file?
>> Couldn't it be done by (temporarily) attaching the standard output to
>> a file descriptor (an already open scratch file)? Just wishful
>> thinking, I guess.
> If you're under Linux, a named pipe might do what you want:
> call system('mkfifo output.fifo')
> call system('./program > output.fifo')
> open(10,file='output.fifo',action='read')
> ! ... read
> close(10)
> there's also a unix C library popen() function that does the same
> thing (with anonymous, memory pipes). Some compilers (e.g. Fortran)
> allow you to call it and convert the descriptor to Fortran unit number.

        There's also mktemp in linux/cygwin/etc if you can coerce system()
to return the temporary filename, i.e. let the OS create the scratch file
rather than do it in Fortran.

        Unfortunately the following doesn't work (the file is created but
I didn't get the name back; I presume the environment executing system()
doesn't persist so SCRFILE disappears).  A unix expert may be able to
suggest how to get the name cleanly.  The fragment of code at the bottom
does work, but uses the method I was trying to avoid -- it risks deleting
a file of the same name as that hard-wired into the code...

      implicit none
      character*128 fname
      real p
      integer status

      call system('export SCRFILE=`mktemp`',status)
      write(*,*)'Status ',status
      call getenv('SCRFILE',fname)
      open(unit=42, file=fname, status='old')
      write(42,*)4*atan(1.0)
      close(42)
      write(*,*)'Closed ',fname
      open(unit=42, file=fname, status='old')
      read(42,*)p
      write(*,*)'Pi is ',p
      close(42,status='delete')
      stop
      end

===
      call system('mktemp>tfile')
      open(unit=43,file='tfile',status='old')
      read(43,'(A128)')fname
      close(43,status='delete')
===

--
Ivan Reid, School of Engineering & Design, _____________  CMS Collaboration,

        KotPT -- "for stupidity above and beyond the call of duty".



Tue, 22 Sep 2009 18:47:23 GMT  
 Redirecting output to a scratch file

Quote:
>         Unfortunately the following doesn't work (the file is created but
> I didn't get the name back; I presume the environment executing system()
> doesn't persist so SCRFILE disappears).  A unix expert may be able to
> suggest how to get the name cleanly.  The fragment of code at the bottom
> does work, but uses the method I was trying to avoid -- it risks deleting
> a file of the same name as that hard-wired into the code...

You can't exchange information between a process and a child process
that way: the child process inherits the environment variables
from its parent, but the parent never gets the environment variables
back from the child. Otherwise you could have very strange
interactions.

(In Windows there does seem to be a two-way communication of
sorts but it does not appear to be very reliable - more a
kind of accident.)

Regards,

Arjen



Tue, 22 Sep 2009 19:56:34 GMT  
 Redirecting output to a scratch file

Quote:
> Hi all,

> >From a fortran program I need to run an external program and read its

> output. I would like to redirect this output to a fortran scratch file
> so that it is deleted when closed and there are no name conflicts. Is
> this, or something similar, possible in a more or less portable way in
> fortran (although I realize the "CALL SYSTEM()" I'm using is not fully
> portable)?

See if something like

      PROGRAM MAIN
        INTEGER I, SYSTEM
        CHARACTER FILNAM*1024, LINE*80
        OPEN(10, ACTION='READ', STATUS='SCRATCH')
        INQUIRE(10, NAME=FILNAM)
        I = SYSTEM('echo HELLO > ' // TRIM(FILNAM))
        READ(10, *) LINE
        PRINT *, TRIM(FILNAM)
        PRINT *, TRIM(LINE)
      END

works for you.  Note that the function SYSTEM is an INTEGER
function.  If you are using Sun f90/f95, SH (also an INTEGER
function) is usually a better choice than SYSTEM.

Bob Corbett



Thu, 24 Sep 2009 12:25:37 GMT  
 Redirecting output to a scratch file
Only read this thread just now.
Write a string with the date and time.
Use the string as the "scratch" file name; it will be unique and you
can write the filename out somewhere.


Fri, 25 Sep 2009 06:48:12 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Redirecting Output to files

2. Redirecting to TWO output files

3. Redirecting program output to file

4. how to redirect lpt1 to file-output

5. redirect g77 output into a file under windows xp

6. redirect output of functions to a string or file

7. Redirect file output into variables

8. redirect g77 under DOS 6.22- need output file

9. expect: redirect output into file called `uname -n'.txt

10. redirect exec output to a file

11. Redirecting output to file with elisp

12. Redirecting TCL script output to a file

 

 
Powered by phpBB® Forum Software