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

发布于 2024-12-21 08:34:07 字数 504 浏览 7 评论 0原文

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

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

编译器版本:

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

全部不再 2024-12-28 08:34:07

如果您有 Fortran 2003 编译器,则内部模块 iso_fortran_env 定义变量input_unitoutput_uniterror_unit分别指向标准输入、标准输出和标准错误。

我倾向于

#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)。

另一种方法是编写您自己的非内在 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

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

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

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

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

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.

野却迷人 2024-12-28 08:34:07

Fortran 标准没有指定哪些单位编号对应于 stdin/out/err。通常的约定(例如 gfortran)是 stderr=0、stdin=5、stdout=6。

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

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.

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.

蓝眸 2024-12-28 08:34:07

stderr 实际上是 0。 5 是标准输入,6 是标准输出。

例如:

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

给出:

./a.out 
Error
Good

虽然

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

我会存储一个 STDERR = 0 的参数以使其可移植,所以如果您遇到不同的平台,您只需更改该参数即可。

此示例是使用 ifort 12.1.1.256、11.1.069、11.1.072 和 11.1.073 编译并运行的。

It's actually 0 for stderr. 5 is stdin, 6 is 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.

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

你好,陌生人 2024-12-28 08:34:07

Fortran 中写入 stdout 的标准方法是用星号代替单元号,即,

WRITE(*,fmt) something

或者简单地使用

PRINT fmt,something

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

READ(*,fmt) something

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

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

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

WRITE(*,fmt) something

or to simply use

PRINT fmt,something

Similarly, the standard way to read from stdin is

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文