Kevin Powell
Kevin Powell

Reputation: 601

Fortran execute_command_line does not return result

I need to create a file in fortran and then read the data in the file.

call execute_command_line('pwd > workdir.dat')

open(unit=10, file='workdir.dat', status='replace', IOSTAT=open_stat)
    if (open_stat /= 0) stop "Reading workdir.dat file Error"
    read(10,"(A)") workdir
close(10)

However, workdir.dat is empty when I try to open it, giving me serious open error. It seems that the system only flush the content of workdir.dat to the file at the end of the program. How do I make sure workdir.dat is ready to use before open ?

Upvotes: 0

Views: 253

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78316

The open statement includes the clause status=replace which, in effect, tells the run time system that to discard the file's contents and write them anew. To be precise, the language standard states wrt to the status specifier on an open statement:

If REPLACE is specified and the file does exist, the file is deleted, a new file is created with the same name, and the status is changed to OLD.

Change the clause to status=old which is the correct specification for this case.

Upvotes: 2

Related Questions