0

I have some issues about opening and reading multiple files. I have to write a code which reads two columns in n files formatted in the same way (they are different only for the values...). Before this, I open another input file and an output file in which I will write my results. I read other questions in this forum (such as this one) and tried to do the same thing, but I receive these errors:

 read(fileinp,'(I5)') i-49
                   1
 devstan.f90:20.24:

 fileLoop : do i = 50,52
                    2
 Error: Variable 'i' at (1) cannot be redefined inside loop beginning at (2)

and

 read(fileinp,'(I5)') i-49
           1
 Error: Invalid character in name at (1)

My files are numbered from 1 to n and are named 'lin*27-n.dat' (where n is the index starts from 1) and the code is:

 program deviation
  implicit none

  character(len=15) :: filein,fileout,fileinp
  integer :: row,i,h
  real :: usv,usf,tsv,tsf,diff


  write(*,'(2x,''Input file .......''/)')
  read(*,'(a12)') filein
  write(*,'(2x,''Output file........''/)')
  read(*,'(a12)') fileout
  open(unit = 30,File=filein)
  open(unit = 20,File=fileout)


  fileLoop : do i = 50,52
  fileinp = 'lin*27-'
  read(fileinp,'(I5)') i-49
  open(unit = i,File=fileinp)


   do row = 1,24
    read(30,*) h,usv,tsv
    read(i,*)  h,usf,tsf

     diff = usf - usv

    write(20,*) diff
   enddo
   close(i)
  enddo fileLoop

 end program deviation

How can I solve it? I am not pro in Fortran, so please don't use difficult language, thanks.

Community
  • 1
  • 1

1 Answers1

0

The troublesome line is

read(fileinp,'(I5)') i-49

You surely mean to do a write (as in the example linked): this read statement attempts to read from the variable fileinp rather than writing to it.

That said, simply replacing with write is probably not what you need either. This will ignore the previous line

fileinp = 'lin*27-'

merely setting to, in turn, "1", "2", "3" (with leading blanks). Something like (assuming you intend that * to be there)

write(fileinp, '("lin*27-",I1)') i-49

Note also the use of I1 in the format, rather than I5: one may want to avoid blanks in the filename. [This is suitable when there is exactly one digit; look up Iw.m and I0 when generalizing.]

francescalus
  • 30,576
  • 16
  • 61
  • 96
  • thanks francescalus, but if my input files are 'lin*27-1' is it only necessary to change I5.5 into I1? because at runtime i receive this error: At line 28 of file devstan.f90 (unit = 50, file = 'lin*27-00001') Fortran runtime error: End of file – Alessio De Luca Dec 07 '14 at 12:59