0

I'm currently porting an application from Fortran to C and need to output some variables to compare results. I'm very new to Fortran, and although i understand the code and have now ported several thousand lines, I'm a noob at writing Fortran code myself.
This code:

  write(6,'(A,I3,A,E12.8,A,E12.8,A,E12.8,A,E12.8,A,E12.8)') 'iHyd:',
 &     ih,'; dzdr: ',dzdr,'; tauray:', tauRay,'; zRay: ',
 &     zray,'; ampRay: ',realpart(aray),'+j*',
 &     imagpart(aray),'; qRay: ',qray,'; width :',w

Compiles fine, but when run, the program exits with:

At line 296 of file calcpr.for (unit = 6, file = 'stdout')  
Fortran runtime error: Expected INTEGER for item 15 in formatted transfer, got REAL  
(A,I3,A,E12.8,A,E12.8,A,E12.8,A,E12.8,A,E12.8)  
   ^  
 q0:    1432.3944878270595     
 nArrayR:                   501 nArrayZ:                   201
iHyd:  1; dzdr: ************; tauray:************; zRay: ************; ampRay:          NaN+j*         NaN
; qRay: 

Besides being really ugly, it doesn't make much sense to me, as ìh is declared as integer*8 and not as real.

So how can i solve this?

Emanuel Ey
  • 2,724
  • 5
  • 30
  • 38

3 Answers3

3

I'm counting 6 character&variable specifications in the format statement, but you're printing 8 of them.

edit:

a nicer use of the format statement would be '(A,I3,7(A,E12.8))'

steabert
  • 6,540
  • 2
  • 26
  • 32
  • 1
    Ok, that solved it, thanks! The `'(A,I3,7(A,E12.8))'` thing is also a neat trick. I wonder though, why it is that the error message was completely unrelated to the problem... And why such a bug doesn't generate a compile-time error, but instead causes a runtime error. Man, Fortran is strange... – Emanuel Ey Mar 21 '11 at 15:07
  • 2
    The error message is related, when the end of the format specifiers is reached and there are more variables to be printed, it starts a new line and wraps around the specfiers, so when it reaches `'; qRay: '`, it start a new line and tries to match `A` again, which works. Then it moves on to `qray` and tries to match `I3`, which fails because qray isn't an integer. – steabert Mar 21 '11 at 17:48
  • Ah, that actually makes sense :) It's just very different from anything I'm used to -thanks for the clarification. – Emanuel Ey Mar 22 '11 at 11:03
1

Fortran "recycles" the format if there are more things to be printed than specified in the format statement. If a write statement gives results you don't understand, to diagonose the problem it may be helpful to remove the things printed one at a time until the error goes away.

Fortranner
  • 2,525
  • 2
  • 22
  • 25
0

It says "item 15", which I would take to be down near the end of your list, not ih at the beginning. It's clear that both "w" and "qray" are being printed as REAL; is either one of them an INTEGER? You may need to change the format specifier then.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186