Did you know that the following statement is guaranteed by one of the fortran standards 90/95/2003? "Suppose that the read statement for a character variable is given by an empty string (that is, it contains only spaces and new string characters). If the format specifier is an asterisk (*), it continues to read subsequent lines until it is empty If the specifier format is '(A)', an empty string is replaced instead of a character variable.
For example, check out the following minimum program and input file.
program code:
PROGRAM chk_read INTEGER, PARAMETER :: MAXLEN=30 CHARACTER(len=MAXLEN) :: str1, str2 str1='minomonta' read(*,*) str1 write(*,'(3A)') 'str1_start|', str1, '|str1_end' str2='minomonta' read(*,'(A)') str2 write(*,'(3A)') 'str2_start|', str2, '|str2_end' END PROGRAM chk_read
input file:
----'input.dat' content is below this line---- yamanakako kawaguchiko ----'input.dat' content is above this line----
Note that there are four lines in 'input.dat', and the first and third lines are empty (contain only spaces and new string characters). If I run the program as
$ ../chk_read < input.dat > output.dat
I get the following output
----'output.dat' content is below this line---- str1_start|yamanakako |str1_end str2_start| |str2_end ----'output.dat' content is above this line----
The first read statement for the variable 'str1' seems to look at the first line of 'input.dat', find the empty line, go to the second line, find the value of the character 'yamanakako' and store it in 'str1'.
In contrast, the second read statement for the variable 'str2' seems to get the third line, which is empty, and store the empty line in 'str2' without going to the fourth line.
I tried compiling Intel Fortran (ifort 12.0.4) and GNU Fortran (gfortran 4.5.0) and got the same result.
A little bit about the background of the question: I am writing a routine to read a data file that uses an empty string as a separator of data blocks. I want to make sure that an empty line and only an empty line are thrown when reading data. I also need to make it standard and portable.
Thanks for your help.