[英]Continuation line trick to adjust Fortran I/O at compile time, with CPP directives, avoiding redundancy
Within some legacy Fortran 77
subroutines I was using the continuation mark &
in position 6 and few cpp
directives to adjust I/O
at compile time - depending on very few parameters - to avoid redundancy.在一些遗留
Fortran 77
子例程中,我使用了 position 6 中的续行符号&
和很少的cpp
指令在编译时调整I/O
- 取决于很少的参数 - 以避免冗余。 Look at a simpler example, here below:看一个更简单的例子,如下:
open(unit=1,file=opfile,status="unknown",form="unformatted")
!
read(1) a, b, c
!
#ifdef _READMORE
& ,d, e
#endif
!
close(1)
Now, after some f90
refactoring (basically, putting almost everything within modules
), I am struggling to reproduce the above mentioned behaviour, ie reading d
and e
if and only if _READMORE
is defined, since the first solution I tried - the one here below - does not work as expected, because of the unexpected &
at the end of the first line, if _READMORE
is not defined:现在,经过一些
f90
重构(基本上,将几乎所有内容都放在modules
中),我正在努力重现上述行为,即当且仅当定义_READMORE
时读取d
和e
,因为我尝试了第一个解决方案 - 下面的一个- 无法按预期工作,因为在第一行末尾出现意外&
,如果_READMORE
未定义:
open(unit=1,file=opfile,status="unknown",form="unformatted")
!
read(1) a, b, c &
!
#ifdef _READMORE
,d, e
#endif
!
close(1)
Could anyone suggest me what would be the best way to accomplish this task?谁能建议我完成这项任务的最佳方法是什么?
It would be appreciable if the proposed solution still relies on this continuation line trick and preserves backward compatibility.如果提议的解决方案仍然依赖于这种延续线技巧并保持向后兼容性,那将是可观的。
I would go for the explicit version:对于显式版本,我会 go :
open(unit=1,file=opfile,status="unknown",form="unformatted")
!
!
#ifdef _READMORE
read(1) a, b, c &
,d, e
#else
read(1) a, b, c
#endif
!
close(1)
The only workaround I found - thus far - is simply adding one more read
statement (coming together with many side effects ):到目前为止,我发现的唯一解决方法是简单地添加一个
read
语句(与许多副作用一起出现):
open(unit=1,file=opfile,status="unknown",form="unformatted")
!
read(1) a, b, c
!
#ifdef _READMORE
read(1) d, e
#endif
!
close(1)
avoiding at all the continuation line trick above.完全避免上面的延续线技巧。 However, if I am not wrong, this would fail reading files written by the original
Fortran 77
subroutine, when _READMORE
is defined, since each read/write
statement adds a newline :但是,如果我没记错的话,当定义
_READMORE
时,这将无法读取由原始Fortran 77
子例程编写的文件,因为每个read/write
语句都会添加一个换行符:
open(unit=1,file=opfile,status="unknown",form="unformatted")
!
write(1) a, b, c
!
#ifdef _READMORE
& ,d, e
#endif
!
close(1)
and unfortunately I have a lot of these files that I would not like to re-write all them this way.不幸的是,我有很多这些文件,我不想以这种方式重新编写它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.