Fortran 90中的标准输入和输出单元?

时间:2021-06-19 00:06:08

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

在Fortran中如何读写标准输入、输出和错误流stdin、stdout和stderr ?我听说给stderr写信,例如,曾经是write(5, fmt=…),其中5是stderr的单位,我知道给stdout写信的方法是使用write(*, fmt=…)。

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

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

Compiler version:

编译器版本:

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

Intel(R) Fortran编译器,用于在Intel(R) 64上运行的应用程序,10.0版本构建20070426包ID: l_fc_p_10.0.023版权(C) 1985-2007 Intel Corporation。保留所有权利

4 个解决方案

#1


35  

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.

如果您有一个Fortran 2003编译器,那么固有模块iso_fortran_env将定义变量input_unit、output_unit和error_unit,它们分别指向标准in、标准out和标准错误。

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

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

在我输入/输出例程。尽管这当然意味着预处理您的源文件(使用ifort来做这个,在编译源代码时使用-fpp标记或将源文件扩展名从.f扩展到.f或从.f90到.f90)。

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:

另一种替代方法是编写您自己的、非固有的iso_fortran_env模块(如果您没有Fortran 2003编译器的话),就像这里讨论的那样(这个链接自从这个答案被发布以来就消失了)。在这个例子中,他们使用一个模块:

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

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.

正如在其他答案中所指出的,0、5和6通常是stderr、stdin和stdout(对于Linux上的ifort是这样的),但这不是Fortran标准所定义的。使用iso_fortran_env模块可以移植到这些单元。

#2


12  

The Fortran standard doesn't specify which units numbers correspond to stdin/out/err. The usual convention, followed by e.g. gfortran, is that stderr=0, stdin=5, stdout=6.

Fortran标准没有规定哪些单元数对应于stdin/out/err。通常的惯例,然后是gfortran,是stderr=0, stdin=5, stdout=6。

If your compiler supports the F2003 ISO_FORTRAN_ENV intrinsic module, that module contains the constants INPUT_UNIT, OUTPUT_UNIT, and ERROR_UNIT allowing the programmer to portably retrieve the unit numbers for the preconnected units.

如果您的编译器支持F2003 ISO_FORTRAN_ENV固有模块,那么该模块包含常量INPUT_UNIT、OUTPUT_UNIT和ERROR_UNIT,允许程序员可移植地检索预连接单元的单元号。

#3


5  

It's actually 0 for stderr. 5 is stdin, 6 is stdout.

stderr等于0。5是stdin, 6是stdout。

For example:

例如:

PROGRAM TEST
  WRITE(0,*) "Error"
  WRITE(6,*) "Good"
END PROGRAM TEST

Gives:

给:

./a.out 
Error
Good

while

./a.out 2> /dev/null
Good

I would store a PARAMETER that is STDERR = 0 to make it portable, so if you hit a platform that is different you can just change the parameter.

我将存储一个STDERR = 0的参数以使其具有可移植性,因此如果您碰到一个不同的平台,您只需更改参数即可。

This example was compiled and run with ifort 12.1.1.256, 11.1.069, 11.1.072 and 11.1.073.

这个示例使用ifort 12.1.1.256、11.1.069、11.1.072和11.1.073进行编译和运行。

#4


0  

The standard way to write to stdout in Fortran is to put an asterisk instead of the unit number, i.e.,

用Fortran写stdout的标准方法是用星号而不是单位号,例如,

WRITE(*,fmt) something

or to simply use

或简单地使用

PRINT fmt,something

Similarly, the standard way to read from stdin is

类似地,从stdin中读取数据的标准方法是

READ(*,fmt) something

There is no standard way to write to stderr unless you use ERROR_UNIT from the ISO_FORTRAN_ENV module, which requires Fortran 2003 or later.

除非使用ISO_FORTRAN_ENV模块中的ERROR_UNIT(这需要Fortran 2003或更高版本),否则没有标准的方法来写入stderr。

Unit numbers 0, 5 and 6 will certainly work in the ifort compiler (and also in some other Fortran compilers), but keep in mind they are compiler-dependent.

单元号0、5和6肯定会在ifort编译器(以及其他一些Fortran编译器中)工作,但是要记住它们是编译器依赖的。

#1


35  

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.

如果您有一个Fortran 2003编译器,那么固有模块iso_fortran_env将定义变量input_unit、output_unit和error_unit,它们分别指向标准in、标准out和标准错误。

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

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

在我输入/输出例程。尽管这当然意味着预处理您的源文件(使用ifort来做这个,在编译源代码时使用-fpp标记或将源文件扩展名从.f扩展到.f或从.f90到.f90)。

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:

另一种替代方法是编写您自己的、非固有的iso_fortran_env模块(如果您没有Fortran 2003编译器的话),就像这里讨论的那样(这个链接自从这个答案被发布以来就消失了)。在这个例子中,他们使用一个模块:

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

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.

正如在其他答案中所指出的,0、5和6通常是stderr、stdin和stdout(对于Linux上的ifort是这样的),但这不是Fortran标准所定义的。使用iso_fortran_env模块可以移植到这些单元。

#2


12  

The Fortran standard doesn't specify which units numbers correspond to stdin/out/err. The usual convention, followed by e.g. gfortran, is that stderr=0, stdin=5, stdout=6.

Fortran标准没有规定哪些单元数对应于stdin/out/err。通常的惯例,然后是gfortran,是stderr=0, stdin=5, stdout=6。

If your compiler supports the F2003 ISO_FORTRAN_ENV intrinsic module, that module contains the constants INPUT_UNIT, OUTPUT_UNIT, and ERROR_UNIT allowing the programmer to portably retrieve the unit numbers for the preconnected units.

如果您的编译器支持F2003 ISO_FORTRAN_ENV固有模块,那么该模块包含常量INPUT_UNIT、OUTPUT_UNIT和ERROR_UNIT,允许程序员可移植地检索预连接单元的单元号。

#3


5  

It's actually 0 for stderr. 5 is stdin, 6 is stdout.

stderr等于0。5是stdin, 6是stdout。

For example:

例如:

PROGRAM TEST
  WRITE(0,*) "Error"
  WRITE(6,*) "Good"
END PROGRAM TEST

Gives:

给:

./a.out 
Error
Good

while

./a.out 2> /dev/null
Good

I would store a PARAMETER that is STDERR = 0 to make it portable, so if you hit a platform that is different you can just change the parameter.

我将存储一个STDERR = 0的参数以使其具有可移植性,因此如果您碰到一个不同的平台,您只需更改参数即可。

This example was compiled and run with ifort 12.1.1.256, 11.1.069, 11.1.072 and 11.1.073.

这个示例使用ifort 12.1.1.256、11.1.069、11.1.072和11.1.073进行编译和运行。

#4


0  

The standard way to write to stdout in Fortran is to put an asterisk instead of the unit number, i.e.,

用Fortran写stdout的标准方法是用星号而不是单位号,例如,

WRITE(*,fmt) something

or to simply use

或简单地使用

PRINT fmt,something

Similarly, the standard way to read from stdin is

类似地,从stdin中读取数据的标准方法是

READ(*,fmt) something

There is no standard way to write to stderr unless you use ERROR_UNIT from the ISO_FORTRAN_ENV module, which requires Fortran 2003 or later.

除非使用ISO_FORTRAN_ENV模块中的ERROR_UNIT(这需要Fortran 2003或更高版本),否则没有标准的方法来写入stderr。

Unit numbers 0, 5 and 6 will certainly work in the ifort compiler (and also in some other Fortran compilers), but keep in mind they are compiler-dependent.

单元号0、5和6肯定会在ifort编译器(以及其他一些Fortran编译器中)工作,但是要记住它们是编译器依赖的。