为什么在Fortran中使用命令PRINT覆盖输入文件? [英] Why does using command PRINT in Fortran overwrite the input file?

查看:205
本文介绍了为什么在Fortran中使用命令PRINT覆盖输入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码,并在Fortran中使用输入和输出功能.代码如下(仅用于简化):

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(1000):: A
  REAL:: B
  INTEGER::T

 !Defining input and output  
  OPEN(UNIT=1, FILE='input.dat', STATUS='OLD')
  OPEN(UNIT=2, FILE='output.dat', STATUS='NEW')  

 !Reading from file "input.dat"  
  READ(1,*) (A(I),I=1,1000)

 !Just for initial condition
  B=0.0  

  DO T=1, 10
    PRINT *, 'Step =', T 
        DO I=1, 1000     
           B=B+1.0     
           A(I)=A(I)/B  
        END DO
  END DO

 !Writing results into file "output.dat"
   DO I=1, 1000
      WRITE (2,100) I, A(I)
   END DO 
   100 FORMAT (' ',T3, I12, T17, F14.4)   

END PROGRAM TEST

我正在使用Gfortran 5.3,结果与我所期望的不同.我希望在程序运行并将屏幕上的变量IA(I)写入文件output.dat时,在屏幕上(或Ubuntu OS中的终端)获取变量T的结果.我对变量IA(I)没问题,因为它们已成功写入文件output.dat中.问题出在变量T上,它没有出现在终端上,但已写入文件input.dat中.好了,即使文件input.dat中的先前文件也没有被覆盖.有人可以给我建议吗?

I was using Gfortran 5.3 and the result was not like what I was expecting. I expected to obtain the result of variable T on the screen (or terminal in Ubuntu OS) when the program is running and the variables I and A(I) are written into file output.dat. I didn't have problem with the variables I and A(I), since they were successfully written into file output.dat. The problem is with variable T, where it didn't appear on the terminal, but it was written into the file input.dat. Well, even the previous file in file input.dat were not overwritten. Could anyone give me the suggestion?

仅供参考,我还尝试了其他编译​​器(使用Windows OS),例如:

FYI, I have also tried on other compiler (using Windows OS), e.g.:

  1. Microsoft Fortran Powerstation(非常老的版本):但是它的工作与我预期的一样.
  2. MinGW-w64(适用于Windows的GCC版本):但无法正常工作.

推荐答案

这可能是因为在平台/编译器/编译器版本/编译器选项的特定组合下,单元1是控制台的预连接单元.

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.

您的OPEN语句将该单元定向到您的输入文件.因此,隐式寻址该单元的PRINT语句然后将其输出定向到同一文件.

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.

使用不同的单元号-从编译器预连接的单元中选择大于10的值通常是安全的.为了进一步安全,您可以使用INQUIRE(UNIT=unit_number, EXIST=some_logical_variable)语句在OPEN语句之前检查特定单元是否已连接到文件-如果是,请选择其他单元号.理想情况下,如果您正在编写Fortran 2008,则可以使用NEWUNIT说明符.

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

这篇关于为什么在Fortran中使用命令PRINT覆盖输入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆