Read(1,"(<n>i10)") problem


I have an old code. It has this line. Can’t compile it with gFortran. How do I change it?

Read(1,"(<n>i10)") (a(J),J=1,n)

N is read in before this line.

Thanks for help.



I guess you mean:

read(1,"(<n>i10)") (a(j), j =1,n)

as the line you give is standard Fortran (even as old as FORTRAN 77)… You could try:

read(1,"(*(i10))") (a(j), j =1,n)

The asterisk indicates an unlimited repetition of the format group that follows (hence the extra parentheses).

The use of <n> is a non-standard extension, which only a few Fortran compilers supported, notably Dec/Vax.
I would suggest you think about the problem and perhaps solve it in a different way to what Dec provided.
How big can “n” be is one issue, but to me the important issue is how big/long a data line do you want to prepare ?

Perhaps a compromise as 10 numbers to a line (100 characters) might be a better way to start.
read (1,fmt=‘(10I10)’ ) (a(j),j=1,n)
This may provide a more manageable data set to prepare, than perhaps when n=100 where the line of data is 1000 characters.

Also, some compilers supported a comma to terminate each 10-character field, so each “I10” field could be terminated with a comma, so reducing the length of the data line. I am not sure which compilers support this non-standard extension, but it was a feature I used extendively when preparing data.

Another alternative is to use list-directed I/O where no fixed field formatting is used. This can be much better when typing the data into a file, however if the data is being generated by another Fortran program, a fixed field length layout is more reliable and less “error-prone” approach.

I am also puzzled by the use of file unit = 1. On some compilers, this is terminal input, so the data interface may be different to what I am suggesting.

For the record, the gfortran documentation mentions this extension here, which it does not support. The workaround suggested is not exactly your case, but you probably also have write statement with variable format expression in your legacy code.

[quote=“Cean, post:1, topic:7791”]
Read(1,“(i10)”) (a(J),J=1,n)

ian@ian-Latitude-E7440:~$ nano readb.f
ian@ian-Latitude-E7440:~$ gfortran readb.f -ffree-form
ian@ian-Latitude-E7440:~$ ./a.out
           1           2           3           4           5           6           0           0           0           0           7           6
ian@ian-Latitude-E7440:~$ cat readb.f
integer :: J,i
integer a(10), n
n = 10
do i = 1, 10, 1
 a(i) = 0
end do
Read(5,"(i10)") (a(J),J=1,n)
print *,a,  j, n
end program

The above code should work. Replace the quotation marks with ", and the 1 with 5, to input from the screen and keyboard.

I would try just

Read(1,*) a

which reads the entire array a with a list-directed read, but you should temporarily add a debugging line


to check that the data is being read properly from the file.

Another possibility, which is closer to the original code, is

Read(1,"(*(i10))") a

which means to read each element of a using the i10 format. A final possibility is to create a format string such as “(5i10)” (if for example n = 5) at run-time using an internal write.

Yes. Never see it before. And (1,“(*(i10))” works.

(1,*) also works. This is what I familiar with. Don’t know the former can come down to this.

Thanks all.

The difference between (1,“(*(i10))” and (1,*) will hit you if any of the integers are really large. With most compilers huge(1) has 10 digits. An input file intended for the former might say 1234567890111111111, but that won’t do what you want with the latter. On the other hand, -1234567890 input would be OK with the latter but not the former.
That is one reason why iostat and iomsg were invented.