Why does using command PRINT in Fortran overwrite the input file?

后端 未结 2 1658
清歌不尽
清歌不尽 2021-01-28 22:38

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(         


        
2条回答
  •  半阙折子戏
    2021-01-28 23:18

    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.)

提交回复
热议问题