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

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

问题描述

如何读写标准输入,输出和错误流 stdin stdout 和<$ Fortran中的c $ c> stderr ?我听说写过 stderr ,例如 used write(5,fmt = ...) ,其中 5 stderr 的单位,我知道写入 stdout 是使用 write(*,fmt = ...)



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



编译器版本:


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


解决方案

如果您有Fortran 2003编译器, =http://fortranwiki.org/fortran/show/iso_fortran_env =noreferrer> iso_fortran_env 定义变量 input_unit output_unit error_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 

! Lahey / Fujitsu Fortran for Linux的非内部版本。
!请参阅Fortran 2003标准的子条款13.8.2。

隐含NONE
public

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

end module iso_fortran_env

正如其他答案中所指出的,0,5和6通常是 stderr stdin stdout (Linux上的 ifort 也是如此),但这是不是由Fortran标准定义。使用 iso_fortran_env 模块是可移植写入这些单元的正确方法。


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

解决方案

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.

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

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