在不知道适当的RECL的情况下,如何使用Fortran无格式直接访问来访问单个记录? [英] How to access a single record with Fortran unformatted direct-access when the appropriate RECL is not known?

查看:78
本文介绍了在不知道适当的RECL的情况下,如何使用Fortran无格式直接访问来访问单个记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非使用F2003中提供的新式流访问功能,否则Fortran通常会将文件视为记录序列.如果连接了文件以进行直接访问,则可以通过指定记录号以任何顺序访问任何记录.例如:

Unless using the new-ish stream-access available in F2003, Fortran classically considers files to be a sequence of records. If a file is connected for direct-access one can access any record in any order by specifying the record number. For example:

open(newunit=funit, file=filename, form='unformatted', access='direct', &
     recl=64, status='old')
read(funit, rec=2) data
close(funit)

这听起来不错...但是,我不确定我是否了解RECL参数,以及在尚不知道正确的记录长度的情况下如何有效使用直接访问.从文档(各种Intel Fortran版本)中:

So this sounds great...however, I'm not sure I understand the RECL parameter and how direct-access can be used effectively if the correct record length isn't already known. From the docs (various Intel Fortran versions):

所有记录的长度都由OPEN语句中的RECL选项指定.

All records have the length specified by the RECL option in the OPEN statement.

换句话说,直接访问允许以等于或小于RECL的量访问数据,同时以RECL的增量在文件中移动.也就是说,您可以指定自己喜欢的任何值(我假设等于或小于文件的大小).我想这在事后看来很明显……但是我希望可以通过某种方式发现适当的RECL. 1 也许我做错了,但是我只想从指定的记录中获取数据-不多,不少.

In other words, direct-access allows access to data in an amount equal to or less than RECL, while moving through the file in increments of RECL. That is, you can specify any value you like (equal to or less than the size of the file, I assume). I guess that's obvious in hindsight...yet I was hoping that the appropriate RECL was discoverable in some way.1 Perhaps I'm doing this wrong, but I would like to get the data from the specified record only - not more, not less.

除了在文件的标头"部分中编码相应的 RECL 值外,还有一种方法可以同时访问未连接(甚至已格式化)的文件的单个记录如果事先不知道正确的记录长度,则直接访问?用什么交易技巧来做到这一点?

Aside from encoding the appropriate RECL value in a 'header' section of the file, is there a way to access a single record at a time with a file connected for unformatted (or even formatted) direct-access if the correct record length is not known beforehand? What tricks-of-the-trade are used to do this?

1 inquire(funit,recl = rl)提供适当的RECL,但是如果该文件已连接以便直接访问,它将返回打开文件时指定的RECL值.如果为顺序访问而连接,则它似乎返回允许的最大记录长度(?),在我的情况下为2040.

1 I had hoped inquire(funit, recl=rl) would provide the appropriate RECL, but if the file was connected for direct-access, it returns the RECL value specified when the file was opened. If connected for sequential-access, it seems that it returns the maximum record length allowed (?), 2040 in my case.

推荐答案

实际上,不可能通过查看文件来找到它,因为那只是数据,(通常)没有记录标记,因此编译器只是看到非结构化字节流.至少在面向字节的计算机中.我对面向记录的文件系统一无所知,只有它们存在.

Indeed, it is not possible to find it out from looking at the file, because that is just the data and (normally) no record markers, so the compiler just sees a stream of unstructured bytes. At least in byte oriented computers. I know nothing about record oriented filesystems, only that they exist.

如果您知道直接访问记录中存储了哪种数据,则可以通过询问有关文件的信息而不是询问文件来查询编译器.

If you know what kind of data is stored in the direct access record, you can inquire the compiler by asking not about the file, but about the data.

例如,如果记录由变量 a b c 组成,则无论它们是什么,

For example, if the record consists of variables a, b, c, whatever they are,

 !just an example
 real :: a(10)
 type(my_type) :: b
 character(5) :: c(3)

您问这样的记录有多大

 inquire(iolength=rl) a, b, c

,然后使用 recl = rl

open(newunit=funit, file=filename, form='unformatted', access='direct', &
     recl=rl, status='old')

例如,参见>为什么直接访问我/O与Intel Visual Fortran不能正常工作

请注意,RECL值不可移植,并且在编译器之间会有所不同.有些以字节为单位进行度量,有些则以4字节为单位.我只记得gfortran和ifort是不同的,而不是哪一个.而且我不在乎哪一个.

Be careful, the RECL value is not portable and will vary between compilers. Some measure it in bytes and some in 4-byte words. I just remember that gfortran and ifort differ, not which one is which. And I don't care which one is which.

如果您发现自己使用 recl = 64 中的魔术常数指定RECL,则说明您做错了,因为这在其他编译器中不起作用.您应该始终有一个变量,而不是固定的数字.

If you find yourself specifying RECL with a magic constant as in recl=64 you are doing something wrong, because this will not work in a different compiler. You should always have a variable, not a fixed number.

这篇关于在不知道适当的RECL的情况下,如何使用Fortran无格式直接访问来访问单个记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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