program dq ! File: http://ftp.cac.psu.edu/pub/ger/fortran/hdk/dq.f90 ! ! Question by deltaquattro at comp.lang.fortran on 4 December 2006. ! ! Q: How can I read data that is mixed real numbers and character ! Strings? ! ! A: Use the intrinsic functioin VERIFY to match all characters that ! make up a real number string. ! character(20) :: string integer :: eof real :: rval open(unit=50,file='dq.in',action='read') do read(50,'(A)',iostat=eof) string if(eof /=0 ) then exit else ! The following VERIFY will be zero for strings with only characters ! represented by its second argument. if(verify(string,'1234567890. -+Ee') == 0) then read(string, *) rval print *, 'Real value: ',rval else print *, 'Character String: ',trim(string) endif endif end do ! Sample input: ! Edward ! 2.0 ! Jeff ! 1.0e-12 ! ! Output for Sample input: ! Character String: Edward ! Real value: 2. ! Character String: Jeff ! Real value: 1.E-12 end program dq