6

I'm trying to compile a piece of code with gfortran and it's failing with the following error:

Error: Nonnegative width required in format string at (1)
../src/powmes.f90:410.20:
     write(lunit,'(I,E,E,E)') wavenum(k),power(k),nmodes(k),errorexpan(k)

414   if (filepower_fold(1:1) /= '#') then
415      fileout=trim(filepower_fold)//'.waven'
416      if (verbose) write(*,*) 'Output '//trim(fileout)
417      open(file=fileout,form='formatted',status='unknown',unit=lunit,err=2)
418      do k=0,ngrid/2
419         do ifold=0,nfoldpow-1
420            write(lunit,'(I,$)') waven(k,ifold)
421         enddo
422         write(lunit,'(I)') waven(k,nfoldpow)
423      enddo
424      close(lunit)

How can I compile this?

francescalus
  • 30,576
  • 16
  • 61
  • 96
Adrian Muresan
  • 199
  • 1
  • 1
  • 7

2 Answers2

10

As already answered, you need to specify widths. Something like ES14.5 might work well for the floating point format. There is a short cut for the integer format: I0 will cause the compiler to use the number of digits needed.

M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • 8
    With gfortran, I wouldn't hesitate to use `g0` format for both. – Vladimir F Героям слава Jun 15 '12 at 14:08
  • 1
    @VladimirF, thanks for that suggestion! That ended up helping me out quite a bit with a similar problem: http://stackoverflow.com/q/19503960/289099 – pattivacek Nov 12 '13 at 16:23
  • @VladimirF do you think `g0` can help for this question: https://stackoverflow.com/questions/44860322/gfortran-requires-format-widths-while-ifort-doesnt ?? is `g0` a compiler option or something that must be included in the source code itself? – user32882 Jul 01 '17 at 11:45
  • 1
    This program is using extensions - omitting widths for I and using the $ format (which suppresses a newline.) G0 is standard Fortran 2003. It was extended to allow G0.d in F2008 and extended again to allow use with character values in F2018. – Steve Lionel Mar 15 '18 at 17:36
8

Try changing the format string I to Iw where w is a positive number. Same with E, only use Ew.d.
For explanation see, for example, this link: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html

Beware though: using, say, I3 for writing out 1234 might print ***, so make sure your formats are wide enough.

EDIT: See @M.S.B.'s answer on how to avoid problems with integer formats.

ev-br
  • 24,968
  • 9
  • 65
  • 78