2
votes

I am trying to compile someone else's FORTRAN code using gfortran 4.4.6. The original coder used something like Compaq FORTRAN.

The code in question is supposed to read a filename like 'foo.txt' and create a new file called 'foo_c.txt':

    file_label_end = SCAN(filename, '.') - 1
    WRITE (output_filename,5) filename
    5 FORMAT (A<file_label_end>, '_c.txt' )

gfortran complains about the opening angle bracket of the character specifier. That is, rather than "A3" he has "A<(a variable with value 3)>". I cannot find any information about interpolating format widths from variables... Is this possible? If not, what's the best way to fix this?

Update:

This seems to be working (compiling):

file_label_end = SCAN(par_filename, '.', .TRUE. ) + 1
output_filename = par_filename(1:file_label_end) // '_c.par'

but I later have a similar case:

12 FORMAT (<n-1>F10.5)
...
READ(1,12) (cat_parm (blk,j), j = 1,n-1)

which I attempted to fix by creating a format string:

write(fmt12,'(I0,A4)') n-1, 'F10.5'
!12 FORMAT (fmt12)
12 FORMAT (fmt=fmt12)

But the "t" in "fmt" gets flagged with an error about "Nonnegative width required in format string"

2

2 Answers

2
votes

The use of <> in Fortran edit descriptors is non-standard, though widely implemented. You certainly can build edit descriptors from variables, the standard way is through an internal write, something like this (omitting declarations):

format_string = ''
write(format_string,'(a1,i0,a6)') 'A', file_label_end,'''_c.txt''' ! WARNING I haven't tested this

and then

write(output_filename,fmt=format_string) filename

But in your case this is not entirely necessary. You could try this instead

file_label_end = SCAN(filename, '.') - 1
WRITE (output_filename,'(a)') filename(1:file_label_end)//'_c.txt'

With the 'a' edit descriptor omitting a width means that all the characters in the expression will be written. You may want to declare

character(len=*), allocatable :: output_filename

if you haven't already done so.

0
votes

The easiest is to use modern fortran

WRITE (output_filename,'(a)') trim(filename) // '_c.txt'

Otherwise I would write the number in the format string using a similar internal write.