很多年前我使用过FORTRAN,并且最近负责维护一个旧的FORTRAN程序(F77).以下代码不熟悉:
READ(FILE_LOG_UNIT, IOSTAT=FILE_STATUS) NUM_WORDS,
. ( BUFFER(BIX), BIX=1, NUM_WORDS )
Run Code Online (Sandbox Code Playgroud)
回顾一些在线论坛发现,让我感到困惑的部分,即延续线,是一个隐含的循环.由于我的程序在这里给我带来麻烦,我想将其转换为传统的DO循环.转换它也可能有助于下一个从现在起5年后冷却这个东西的可怜的邋!!无论如何,我对DO循环等效的最佳猜测是
READ(FILE_LOG_UNIT, IOSTAT=FILE_STATUS) NUM_WORDS
DO BIX=1, NUM_WORDS
READ(FILE_LOG_UNIT, IOSTAT=FILE_STATUS) BUFFER(BIX)
ENDDO
Run Code Online (Sandbox Code Playgroud)
但是,当我只进行此更改时,正在运行的测试用例停止工作.我仍然觉得这里发生的是两个不同的READ(第一个获得NUM_WORDS,第二个循环数据),所以我尝试了一个不那么激烈的变化,将其转换为两个语句但保留隐含的循环:
READ(FILE_LOG_UNIT, IOSTAT=FILE_STATUS) NUM_WORDS
READ(FILE_LOG_UNIT, IOSTAT=FILE_STATUS) ( BUFFER(BIX), BIX=1, NUM_WORDS )
Run Code Online (Sandbox Code Playgroud)
但只是这种变化也会导致良好的测试用例失败.在我的两个改动中,NUM_WORDS的值正如预期的那样,所以看起来循环就是它失败的地方.
那么,原始隐含循环的等效DO循环是什么?或者我完全走错了路?
谢谢
How is the file opened? I.e. is it ACCESS='sequential', access='direct', or access='stream' (well, the last is unlikely as it's a F2003 addition). Secondly, is it formatted or unformatted? I'm going to assume it's unformatted sequential since there is no REC= specifier nor a format specifier in your read statements.
The reason why what you're trying to fails is that each read statement reads a separate record. Prior to the introduction of access='stream' in F2003, Fortran I/O was completely record based, which is slightly alien to those of us used to stream type files as seen in most other languages.
Records for unformatted sequential files are typically separated by "record markers" at each end of the record, typically 4 bytes specifying the length of the record. So the record (on disk) likely looks something like
| length (4 bytes) | num_words (4 bytes?) | buffer(1) | buffer(2) | ... | length |
Now if you try to read, say, num_words with one READ statement, it will correctly read num_words from the file, THEN it will skip forward until the start of the next record. And when you then try to read buffer with a separate READ statement you're already too far ahead in the file.
If you cheat a bit and use F90+ array syntax, you might get away with
READ(FILE_LOG_UNIT, IOSTAT=FILE_STATUS) NUM_WORDS, BUFFER(1:NUM_WORDS)
Run Code Online (Sandbox Code Playgroud)
(尽管我不确定是否允许您在写入的同一语句中引用 num_words )
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |