Wrong action of Open command in Fortran( Insted read open command opens .txt file )

108 Views Asked by At

This is my simple code:

Program Example_Code

Implicit none

Integer :: iERR

  Open( Unit = 15, File = 'Read_Something.txt', Action = 'Read', Status = 'Unknown', iostat = iERR )

    If ( iERR /= 0 ) stop ( "There is no file Read_Something.txt !!!")


  Close( 15, Status = 'Keep')

End Program Example_Code

In the project directory I did not create any .txt file( because I want to test opening file with iostat) and afther the program ends I find Read_Something.txtin the project folder.

What is wrong in this example? Why does this obvious error occur?

IDE: Code::Blocks 17.12, TDM_GCC_5.0.1 - 03

OS: Win 10 X64

1

There are 1 best solutions below

1
On BEST ANSWER

You use status="unknown", that means the compiler can create the file for you so that you can read from it. If you want to open a file only when it exist, use status="old".

BTW, I find it quite useless to explicitly specify any ="unknown", just leave the argument out completely. But that is my personal style.