打开二进制文件用Fortran:现状,形式,访问 [英] Opening Binary Files in Fortran: Status, Form, Access

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

问题描述

我一直在与Fortran的几年,但文件I / O仍是朦胧的我。我的状态理解格式访问 RECL 是有限的,因为我只需要的某些使用情况在读研究生。结果
我知道的Fortran二进制文件具有描述该文件的大小的文件的顶部的额外信息。但是,这从来都不是我的问题,因为之前我只不得不处理用Fortran code,其中的额外信息是必要的,但无形的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头信息平坦,二进制文件?更重要的是,这里是一个很好的链接更详细地描述这些术语:状态格式访问 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语言90分之77,打开一个标准的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语言假定了很多默认的打开参数。事实上,我们最初的例子可以写成下面详细的形式显示所有被假定为默认值。

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

让我们来看看每个参数:

Let us look at each argument:


  • FORM 定义,如果一个文件包含文本(表='格式化')或二进制数据(表='格式化')。

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

访问定义,如果你是从文件中顺序读取数据(访问='连续')或以任何顺序您想(访问=直接)。

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 =老。同样,如果您知道该文件不存在,你将要使用:状态=新

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中九十○分之七十七打开语句的详细信息,有一个很好的资源联机:

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

主教大学林金森一个漂亮的网页(非常感谢你)。

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

<一个href=\"http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlf101a.doc%2Fxlflr%2Fopen.htm\">Slightly由IBM为它的更多官方文件的编译器。

Slightly more official documentation by IBM for it's compilers.

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

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