在单元 5 或 6 上打开文件 [英] Opening a file on unit 5 or 6

查看:9
本文介绍了在单元 5 或 6 上打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Fortran 代码段中进行了如下的读/写操作

I have a read/write operation going on in the Fortran code snippet as follows

OPEN(5,FILE='WKDAT.dat', STATUS='OLD')
OPEN(6,FILE='WKLST.dat', STATUS='UNKNOWN') 

我知道默认情况下,单元号 5 用于从键盘输入,单元号 6 用于在屏幕上显示.我也可以使用 *.

I know that by default the unit number 5 is used for input from the keyboard and unit number 6 is used to display on the screen. Also I can use *.

但在上述 Fortran 代码中,单元号为 5,并给出了文件名WKDAT.dat".所以这意味着数据是从WKDAT.dat"文件中读取的.还有代码单元号 6 和文件名WKLST.dat".所以这意味着数据正在写入WKLST.dat"文件.我的理解正确吗?

But in the above-mentioned Fortran code unit number is 5 and a file name "WKDAT.dat" is given. So this means that the data is being read from "WKDAT.dat" file. Also there is code unit number 6 and a file name "WKLST.dat" is given. So this means that the data is being written to "WKLST.dat" file. Is my understanding correct?

根据我的基本知识:单元号 5 仅用于从键盘获取输入单元号 6 仅用于打印到控制台,因此不应涉及任何文件.但在代码片段中,它同时具有单元号 5、6 以及文件名.

As per my basic knowledge: Unit number 5 is only used to take input from keyboard & unit number 6 is only used to print to console so no files should be involved. But in the code snippet it has both unit number 5, 6 as well as file name.

所以两者都是矛盾的:(

So both are contradicting :(

在此链接中 http://www.oc.nps.edu/~bird/oc3030_online/fortran/io/io.html 他们提到了以下当 I/O 是文件时,您必须将 UNIT 编号(您选择)与 FILENAME 相关联.使用5 和 6 以外的任何单元号.在某些计算机上,某些单元号保留供计算机操作系统使用.

In this link http://www.oc.nps.edu/~bird/oc3030_online/fortran/io/io.html they have mentioned the following "When I/O is to a file you must ASSOCIATE a UNIT number (which you choose) with the FILENAME. Use any unit number other than 5 and 6. On some computers, some unit numbers are reserved for use by the computer operating system."

推荐答案

Fortran 没有神奇的单位编号.Fortran 标准没有说明 5、6 或任何其他用于特殊目的的有效单元号.因此,您可以自由使用 open 语句将任何有效的单元号与文件相关联.但是,正如您所说,由于传统上早于 me 5 和 6 的原因,它已经与键盘和屏幕预先关联.现在您仍然可以通过使用 open 语句来更改关联,这很好,除了它可能导致的混乱,所以我认识的大多数人建议避免这种情况并使用 10 及以上的单位编号.此外,因为 5 和 6 不能保证与默认输入和输出设备相关联,我建议不要使用它们,更喜欢 * 或者,在更现代的代码中,命名常量 input_unit, output_uniterror_unit 来自 iso_fortran_env 内部模块.

Fortran has no magic unit numbers. The Fortran standard says nothing about 5, 6 or any other valid unit number being used for a special purpose. As such you are free to use the open statement to associate any valid unit number with a file. However traditionally for reasons that pre-date me 5 and 6 have been pre-associated with the keyboard and screen, as you say. Now still you can change the association by use of the open statement and that is fine save for the confusion it can cause, so most people I know recommend avoiding this and using unit numbers of 10 and upwards. Also because 5 and 6 are not guaranteed to be associated with the default input and output devices I would recommend against their use, preferring * or, in more modern code, the named constants input_unit, output_unit and error_unit from the iso_fortran_env intrinsic module.

所以总而言之,您的想法是正确的,我并不惊讶您会感到困惑.

So in summary you've got the right idea, and I'm not surprised you're confused.

这篇关于在单元 5 或 6 上打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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