problem with internal file I/O

Hi all,
the following program does not behave as I would expect:

program test
 implicit none
 character(len=100) :: value
 integer, dimension(100) :: intvalues
 integer i

 value = "2 5 69"
 read(value,*,end=20,err=20) (intvalues(i),i=1,100)
20 i=i-1
 write(*,*) i
 write(*,*) intvalues(:i)
end program test

I would expect that the internal read stops at the end of the string and i=3. However, it runs until the end of the implied do-loop. Any suggestions?

Hmm. So what you really want is something like C sscanf behavior that returns the number of matches. Not sure you can get that from Fortran list-directed i/o. I’ll look into it a bit more. In the meantime, for a work-around:

program test
implicit none
character(len=100) :: value
integer, dimension(100) :: intvalues
integer, parameter :: INI = -999
integer i

intvalues = INI
value = “2 5 69”
read(value,*,end=20,err=20) (intvalues(i),i=1,100)
20 i = minloc(intvalues,dim=1,mask=(intvalues.eq.INI)) - 1
end

Just choose a value for INI that is not ever read…