I\'m writing my code and using input and output feature in Fortran. The code looks like this (only for simplification):
PROGRAM TEST
REAL, DIMENSION(
I've found the answers. Actually the code I've posted above will run well on Gfortran 5.3, since I used OPEN(UNIT=1,...)
and OPEN(UNIT=2,...)
which is there will be no problem since I am using 1
and 2
. I just wrote this simple case to represent my real code without checking it first. But actually in my real code, I used two statements that OPEN(UNIT=5,...)
and OPEN(UNIT=6,...)
existed, which are not allowed in Fortran, since:
UNIT=5
declares Standard In
which is used to read in data from the keyboard.UNIT=6
declares Standard Out
which is used to print general output to the screen.UNIT=0
declares Standard Error
which is used to print error messages to the screen.I didn't realize before since I'm working on quite old code, so O need to rewrite it into a newer version one. So, in order to avoid the problems, just don't use UNIT=5
, UNIT=6
and UNIT=0
.
This is likely because with your particular combination of platform/compiler/compiler version/compiler options, unit 1 is the preconnected unit for the the console.
Your OPEN statement directs that unit to your input file. Consequently, PRINT statements that implicitly address that unit then direct their output to the same file.
Use a different unit number - choosing values greater than 10 is generally safe from compiler preconnected units. For further safety you can use an INQUIRE(UNIT=unit_number, EXIST=some_logical_variable)
statement to check whether a particular unit is connected to a file ahead of your OPEN statement - and choose a different unit number if so. Ideally, if you are writing to Fortran 2008 you can use the NEWUNIT specifier.
(Don't hard-code the values of unit numbers into your input/output statements - they should always be represented by a variable or named constant, such that the value can be easily set/changed in the one place.)