简体   繁体   English

在编译时调整 Fortran I/O 的续行技巧,使用 CPP 指令,避免冗余

[英]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时读取de ,因为我尝试了第一个解决方案 - 下面的一个- 无法按预期工作,因为在第一行末尾出现意外& ,如果_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM