使用 FORMAT 语句读取换行符
我正在使用类似 FORMAT 的语句为 Fortran 输入和输出编写一个预处理器和后处理器(有理由不使用 FORTRAN 库)。我想正确处理换行符(“/”)。我手头没有 Fortran 编译器。是否有一个简单的算法来计算写入或消耗了多少换行符(这篇文章仅提供阅读示例)
[请假设 FORTRAN 代码中存在类似 FORTRAN77 的心态,并纠正我的任何 FORTRAN 语法]
更新:还没有评论我只能找到一个编译器并自己运行它。如果我没有被击败,我会发布答案。没有人评论我的格式语法错误。我已经更改了它,但可能仍然存在错误
假设数据文件 1
a
b
c
d
等...
(a) READ 命令是否总是消耗换行符?给出
READ(1, '(A)') A
READ(1, '(A)') B
A='a' 且 B='b'
什么
READ(1,'(A,/)') A
READ(1,'(A)') B
(b) B 给出 ? (我会假设“c”)
(c)
READ(1, '(/)')
READ(1, '(A)') A
A 给出什么(是“b”还是“c”)
(d)A 和 B 和 C 给出什么
READ(1,'(A,/,A)') A, B
READ(1,'(A)') C
(我可以假设“a”和“b”吗?和“c”)
(e)A、B 和 C 给出什么
READ(1,'(A,/,/,A)') A, B
READ(1,'(A)') C
(我可以假设“a”、“c”和“d”)吗?
在某些情况下“/”是多余的吗?
I'm writing a preprocessor and postprocessor for Fortran input and output using FORMAT-like statements (there are reasons not to use a FORTRAN library). I want to treat the new line ("/") character correctly. I don't have a Fortran compiler immediately to hand. Is there a simple algorithm for working out how many newlines are written or consumed (This post just gives reading examples)
[Please assume a FORTRAN77-like mentality in the FORTRAN code and correct any FORTRAN syntax on my part]
UPDATE: no comments yet so I am reduced to finding a compiler and running it myself. I'll post the answers if I'm not beaten to it. No-one commented I had the format syntax wrong. I've changed it but there may still be errors
Assume datafile 1
a
b
c
d
etc...
(a) does the READ command always consume a newline? does
READ(1, '(A)') A
READ(1, '(A)') B
give A='a' and B='b'
(b) what does
READ(1,'(A,/)') A
READ(1,'(A)') B
give for B? (I would assume 'c')
(c) what does
READ(1, '(/)')
READ(1, '(A)') A
give for A (is it 'b' or 'c')
(d) what does
READ(1,'(A,/,A)') A, B
READ(1,'(A)') C
give for A and B and C(can I assume 'a' and 'b' and 'c')
(e) what does
READ(1,'(A,/,/,A)') A, B
READ(1,'(A)') C
give for A and B and C(can I assume 'a' and 'c' and 'd')?
Are there any cases in which the '/' is redundant?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
准确地说,由字符
/
表示的斜杠编辑描述符实际上是记录结束说明符。我现在要引用 Fortran 2008 标准草案,我认为自 FORTRAN77 以来编程语言的行为在这方面没有发生变化,但我不确定。我引用标准是因为(a)如果您正在编写一个预处理器和后处理器,我想您会希望它遵守标准而不是对其进行一些模棱两可的解释,这是我所能提供的全部,并且( b) Fortran I/O 语句有很多变体,我只熟悉其中的几个(这不是其中之一)。10.8.2 斜线编辑
1 斜线编辑描述符指示与当前记录之间的数据传输结束。
2 来自为顺序或连接而连接的文件的输入流访问时,将跳过当前记录的剩余部分,并将文件定位在下一条记录的开头。该记录成为当前记录。在输出到为顺序或流访问而连接的文件时,会在当前记录之后创建一个新的空记录;然后,该新记录将成为该文件的最后一条当前记录,并且该文件位于该新记录的开头。
3 对于直接访问连接的文件,记录号会加 1,并且文件位于具有该记录号的记录的开头(如果存在这样的记录),并且该记录成为当前记录。
(注 10.25 可以写入不包含字符的记录如果文件是内部文件或直接访问的连接文件,则输入时可能会跳过整个记录。)
4 重复规范是可选的。斜杠编辑描述符。如果未指定,则默认值为 1。
因此,斜杠可用于读取到部分读取的记录的末尾并完全跳过记录(已作必要修改用于写作)。
如果我编译您的程序(Mac OS X 上的 Intel Fortran 11.1),我会得到以下输出:
这表明,根据我对标准的解释,gfortran 表现出非标准行为。但是,我怀疑如果您的 I/O 操作更加具体,您可能希望在 READ 和 WRITE 中指定记录长度,您也许能够实现您想要的。
To be precise the slash edit descriptor, represented by the character
/
, is actually the end of record specifier. I'm now going to quote from the draft Fortran 2008 standard, I don't think the programming language's behaviour has changed in this respect since FORTRAN77, but I'm not certain. I'm quoting from the standard because (a) if you are writing a pre- and post-processor I guess you will want it to adhere to the standard rather than to some ambiguous explanation of it which is all I could provide, and (b) there are so many variations in Fortran I/O statements that I am really familiar with only a few of them (and this isn't one of them).10.8.2 Slash editing
1 The slash edit descriptor indicates the end of data transfer to or from the current record.
2 On input from a file connected for sequential or stream access, the remaining portion of the current record is skipped and the file is positioned at the beginning of the next record. This record becomes the current record. On output to a file connected for sequential or stream access, a new empty record is created following the current record; this new record then becomes the last and current record of the file and the file is positioned at the beginning of this new record.
3 For a file connected for direct access, the record number is increased by one and the file is positioned at the beginning of the record that has that record number, if there is such a record, and this record becomes the current record.
(NOTE 10.25 A record that contains no characters may be written on output. If the file is an internal file or a file connected for direct access, the record is filled with blank characters. An entire record may be skipped on input.)
4 The repeat specification is optional in the slash edit descriptor. If it is not specified, the default value is one.
So, the slash can be used to read to the end of a partially-read record and to skip a record entirely (mutatis mutandis for writing).
If I compile your program (Intel Fortran 11.1 on Mac OS X) I get the following output:
which suggests that, based on my interpretation of the standard, gfortran exhibits non-standard behaviour. However, I suspect that if you are more specific in your I/O operations, you might perhaps want to specify record lengths in your READ and WRITEs, you may be able to achieve what you want.
由于到目前为止还没有评论,我正在添加我自己的答案。 (如果有人对此有所补充,我会给他们投票)。
程序:
在 gfortran 下编译给出:
问题:
为什么所有输出之间都有额外的新行?读入的字符串中是否有换行符?
As there have been no comments so far, I'm adding my own answer. (If anyone adds light to this I'll give them votes).
Program:
compiled under gfortran gives:
QUESTION:
why are there additional new lines between all the outputs? Is there a newline in the strings that have been read in?
这些新行实际上是 79 个打印空格。使用 LEN_TRIM() 函数来编写如下语句:
写(6,'(a)')a(1:LEN_TRIM(a))
或将字符字段的长度更改为 (1)
Those new lines are really 79 printed spaces. Use the LEN_TRIM() function to your write statements like this:
write(6,'(a)') a(1:LEN_TRIM(a))
or change the length of your character field to (1)