在 Fortran 中打开二进制文件:状态、表单、访问 [英] Opening Binary Files in Fortran: Status, Form, Access

查看:52
本文介绍了在 Fortran 中打开二进制文件:状态、表单、访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Fortran 多年,但文件 I/O 对我来说仍然很模糊.我对statusformaccessrecl的理解有限,因为我只需要 研究生院的某些用例.
我知道 Fortran 二进制文件在文件顶部有描述文件大小的额外信息.但这对我来说从来都不是问题,因为我只需要在 Fortran 代码中处理 Fortran 文件,其中额外的信息是必要的,但不可见.

I have been working with Fortran for years, but the file I/O is still hazy to me. My understanding of status, form, access, recl is limited, because I only needed certain use-cases in grad school.
I know that Fortran binary files have extra information at the top of the file that describe the size of the file. But that has never been an issue for me before because I have only had to deal with Fortran files in Fortran code, where the extra information is necessary, but invisible.

但是如何在 Fortran 中打开一个平面的二进制文件?

过去,我可能会通过执行以下操作使用 Fortran 打开 Fortran 二进制文件:

In the past, I might open a Fortran binary using Fortran by doing something like this:

open(id,file=file_name,status='old',
     +     form='unformatted',access='direct',recl=4,iostat=ok)
      if (ok .ne. 0) then
        write(1,20) id,ok,file_name
                else
        write(1,21) id,file_name

但是对于没有 Fortran 标头信息的平面二进制文件,这将如何改变?更重要的是,哪里是更详细地描述这些术语的好链接:statusformaccessrecl?

But how does this change for a flat, binary file that doesn't have the Fortran header information? More importantly, where is a good link to describe these terms in greater detail: status, form, access, recl?

推荐答案

我讨厌这样做,但我觉得如果我希望在这篇文章中找到答案,那么前进的道路并不清晰.所以这是前进的方向.

I hate to do this, but I feel that if I were hoping to find answers in this post, the way forward would not be clear. So here is the way forward.

短版

在 Fortran 77/90 中,要打开标准的 Fortran 二进制文件,您可以编写:

In Fortran 77/90, to open a standard Fortran binary file you might write:

OPEN (5, FILE="myFile.txt")

但要打开一个平面的非 Fortran 二进制文件,您必须编写类似这样的内容:

But to open a flat, non-Fortran binary file you would have to write something more like this:

OPEN(5, file="myFile.txt", form='unformatted', access='direct', recl=1)

这种差异是因为 Fortran 样式的二进制文件在文件中的每个记录"周围都有一个 4 字节的页眉和页脚.这些页眉/页脚描述了记录中包含的数据的大小.(在最常见的情况下,您遇到的每个二进制文件都只有一条记录.)

This difference is because Fortran-styled binary files have a 4-byte header and footer around each "record" in the file. These headers/footers describe the size of the data contained in the record. (In the most common case, each binary file you encounter will only have one record.)

加长版

Fortran 假设有很多默认的 open 参数.事实上,我们最初的示例可以写成以下详细的形式来显示所有假定的默认值.

Fortran assumes a lot of default open arguments. In fact, our original example can be written in the following verbose form to show all the defaults that were assumed.

OPEN (5, FILE="myFile.txt") 
OPEN (5, FILE="myFile.txt", FORM="FORMATTED", 
     +   ACCESS="SEQUENTIAL", STATUS="UNKNOWN")

让我们看看每个参数:

  • FORM 定义文件是由文本(form='formatted')还是二进制数据(form='unformatted').

  • FORM defines if a file consists of text (form='formatted') or binary data (form='unformatted').

ACCESS 定义您是按顺序(access='sequential')还是按您想要的任何顺序(access='direct').

ACCESS defines if you are reading data from the file in order (access='sequential') or in any order you want (access='direct').

RECL 定义进入每条记录的字节数.例如,recl=1 只是说记录长度是每个 1 字节;也许它们是 1 字节整数.

RECL defines the number of bytes that goes into each record. For instance, recl=1 just says that the record lengths are 1 byte each; perhaps they are 1-byte integers.

STATUS 定义文件是否已经存在.STATUS="UNKNOWN" 参数意味着该文件可能还不存在,但如果不存在,它将被创建.如果您想防止覆盖旧文件的可能性,请使用:STATUS="OLD".同样,如果您知道该文件尚不存在,您将需要使用:STATUS="NEW".

STATUS defines if the file already exists. The STATUS="UNKNOWN" argument means that the file might not exist yet, but if it doesn't it will be created. If you want to protect against the possibility of writing over an old file use: STATUS="OLD". Similarly, if you know the file doesn't exist yet, you will want to use: STATUS="NEW".

更多信息:

这些打开的语句也会对随后的读/写/关闭语句产生影响.在我原来的帖子中,我需要知道如果你打开一个直接访问文件,你必须写入一个直接访问文件.(也就是说,您的二进制文件中不会包含 Fortran 页眉/页脚.)但是,Fortran 的默认功能是创建包含 Fortran 页眉和页脚的顺序访问文件.

These open statements also have an impact on the read/write/close statements that will follow. In my original post, I needed to know that if you open a direct access file you have to write to a direct access file. (That is, there will be no Fortran headers/footers included in your binary.) However, Fortran’s default functionality is to create sequential access files with Fortran headers and footers included.

有关 Fortran 77/90 中 open 语句的更多信息,网上有很好的资源:

For more information on open statements in Fortran 77/90, there are a good resources online:

一个不错的页面,作者是 Bishop 大学的 Lin Jinsen(非常感谢).

A nice page by Lin Jinsen of Bishop University (thank you so much).

IBM 为它的编译器提供的更多官方文档.

这篇关于在 Fortran 中打开二进制文件:状态、表单、访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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