Fortran 90 中的标准输入和输出单元? [英] Standard input and output units in Fortran 90?

查看:18
本文介绍了Fortran 90 中的标准输入和输出单元?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Fortran 中读取和写入标准输入、输出和错误流 stdinstdoutstderr?我听说写给 stderr,例如,usedwrite(5, fmt=...),用 5 stderr的单位,我知道写入stdout的方法是使用write(*, fmt=...).

How can I read and write to the standard input, output and error streams stdin, stdout and stderr in Fortran? I've heard writing to stderr, for example, used to be write(5, fmt=...), with 5 the unit for stderr, and I know the way to write to stdout is to use write(*, fmt=...).

如何使用 ifort 编译器读取和写入标准输入和输出单元?

How do I read and write to the standard input and output units with the ifort compiler?

编译器版本:

英特尔(R) Fortran 编译器,适用于在英特尔(R) 64 版本 10.0 版本 20070426 上运行的应用程序包 ID:l_fc_p_10.0.023 版权所有 (C) 1985-2007 英特尔公司.版权所有

Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version 10.0 Build 20070426 Package ID: l_fc_p_10.0.023 Copyright (C) 1985-2007 Intel Corporation. All rights reserved

推荐答案

如果你有 Fortran 2003 编译器,内在模块 iso_fortran_env 定义了变量 input_unitoutput_uniterror_unit 指向标准输入,分别是标准输出和标准错误.

If you have a Fortran 2003 compiler, the intrinsic module iso_fortran_env defines the variables input_unit, output_unit and error_unit which point to standard in, standard out and standard error respectively.

我倾向于使用类似的东西

I tend to use something like

#ifdef f2003
use, intrinsic :: iso_fortran_env, only : stdin=>input_unit, &
                                          stdout=>output_unit, &
                                          stderr=>error_unit
#else
#define stdin  5
#define stdout 6
#define stderr 0
#endif

在我的输入/输出例程中.虽然这当然意味着 预处理 您的源文件(使用 ifort,在编译源代码时使用 -fpp 标志或将源文件扩展名从 .f 更改为 .F 或从 .f90.F90).

in my input/output routines. Although this of course means preprocessing your source file (to do this with ifort, use the -fpp flag when compiling your source code or change the source file extension from .f to .F or from .f90 to .F90).

另一种方法是编写您自己的非内在 iso_fortran_env 模块(如果您没有 Fortran 2003 编译器),如 here (自发布此答案以来,此链接已失效).在这个例子中,他们使用了一个模块:

An alternative to this would be to write your own, non-intrinsic, iso_fortran_env module (if you don't have a Fortran 2003 compiler), as discussed here (this link has died since this answer was posted). In this example they use a module:

module iso_fortran_env

  ! Nonintrinsic version for Lahey/Fujitsu Fortran for Linux. 
  ! See Subclause 13.8.2 of the Fortran 2003 standard. 

  implicit NONE 
  public 

  integer, parameter :: Character_Storage_Size = 8 
  integer, parameter :: Error_Unit = 0 
  integer, parameter :: File_Storage_Size = 8 
  integer, parameter :: Input_Unit = 5 
  integer, parameter :: IOSTAT_END = -1 
  integer, parameter :: IOSTAT_EOR = -2 
  integer, parameter :: Numeric_Storage_Size = 32 
  integer, parameter :: Output_Unit = 6 

end module iso_fortran_env

如其他答案所述,0、5 和 6通常 stderrstdinstdout (这对于 Linux 上的 ifort 是正确的),但这 不是 由 Fortran 标准定义的.使用 iso_fortran_env 模块是可移植地写入这些单元的正确方法.

As noted in other answers, 0, 5 and 6 are usually stderr, stdin and stdout (this is true for ifort on Linux) but this is not defined by the Fortran standard. Using the iso_fortran_env module is the correct way to portably write to these units.

这篇关于Fortran 90 中的标准输入和输出单元?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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