简体   繁体   中英

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. 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:

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:

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 ):

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 :

      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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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