I do find it inconvenient that the output of
program main
implicit none
character (len=20) :: text
character (len=10) :: str
real :: x
text = "2/13/2021 3.2"
x = -999.0
read (text,*) str,x
write (*,"(a,1x,f0.4)") "'" // trim(str) // "'",x
end program main
is '2' -999.0000
. Sometimes I use a kludge such as replacing / with a character that is unlikely to appear in the file and then replacing that character with /, as in
program main
implicit none
character (len=20) :: text
character (len=10) :: str
real :: x
integer :: i
character (len=1), parameter :: old_char="/",new_char="@"
text = "2/13/2021 3.2"
x = -999.0
do i=1,len_trim(text)
if (text(i:i) == old_char) text(i:i) = new_char
end do
read (text,*) str,x
do i=1,len_trim(str)
if (str(i:i) == new_char) str(i:i) = old_char
end do
write (*,"(a,1x,f0.4)") "'" // trim(str) // "'",x
end program main
which gives the desired output '2/13/2021' 3.2000
. (Admittedly, the American convention of writing dates as mm/dd/yyyy is odd and conflicts with the rest of the world using dd/mm/yyyy, so it is better to write dates as yyyy-mm-dd. But many files you download will have mm/dd/yyyy dates.)
It would be nice if the programmer could tell the compiler that for a certain read, / should be treated as an ordinary character and not an end-of-record marker. Also useful would be the ability to specify characters to be used as a delimiter in a read, since for a date string “2/13/2021” what you typically want to do is read three integers. I use code such as that above with new_char = ","
to transform a string.