tags:

views:

434

answers:

3

I want to save results in a text file. How can I do that? Write command?

A: 

yes, the write command. And the open command to open the file. Something like this, if my rusty FORTRAN memory serves:

OPEN(UNIT=1, FILE=FNAME, STATUS='NEW')
WRITE(UNIT=1,FMT=*) "your data"

Your other option is to simply write to stdout (unit=*) and the redirect the output from the command line (eg: $ myfortranprogram > output.txt).

Bryan Oakley
A: 

If you are on unix/linux (which is likely), then just redirect the output to a file:

a.out > myoutputfile

where a.out is the name of the compiled executable. Alternatively, change your code to write to a file instead of just to the console:

io=22 !or some other integer number
open(io,file="myoutputfile")
write(io,*)myint,myreal
close(io)

or to keep appending the values to an existing file:

open(io,file="myoutputfile",position="APPEND")

but this is only possible in fortran 90, not in fortran 77. Try renaming your .f to .f90 in that case.

Phil H
A: 

Yes, the write command. The details should be in some book, or on the net, but here's a simple example:

OPEN(UNIT=20, FILE='FILENAME.TXT', STATUS='NEW')
C STATUS='NEW' WILL CREATE A NEW FILE IF ONE DOESN'T EXITST, 'REPLACE' WILL
C OVERWRITE OLD ONE
WRITE(UNIT=20, *)(A(I),I=1,10)
CLOSE(UNIT=20)

In fortran77 it was always good practice to avoid low (below 10) unit numbers, because some of them were reserved - depending on the platform, compiler ... generally, start with those above 10.

ldigas