I have read the Standard, a topic on this discourse and the FortranWiki page cited there but frankly speaking, I don’t quite get the way formatted stream file reading is implemented in Fortran.
Suppose I would like to read integer values from a text file, with variable number of values in each line. Say,
1 2 3 4
1213 3456
I’d say this is the very idea of having stream access! Well, not quite . The Wiki page says at the very beginning of Formatted Stream Files section:
Formatted stream I/O is not all that useful; I suspect they were provided to give Fortran essentially the same I/O facilities as C.
Sorry but they do not give anything close to C. Simplest possible C program:
#include <stdio.h>
int main(int argc, char *argv[])
{
int m, res;
FILE *fp=fopen("test.txt","r");
while (1) {
res=fscanf(fp,"%d",&m);
if (res == EOF) break;
printf("%d\n", m);
}
return 0;
}
happily reads all the values:
$ gcc test_read.c && ./a.out
1
2
3
4
1213
3456
So let’s try Fortran:
program t
implicit none
character(1) :: ch
integer :: iu, m
open(newunit=iu, file='test.txt', access='stream', form='formatted')
do
read(iu, *, end=333) m
print *,m
enddo
333 continue
end program t
$ gfortran read_stream.f90 && ./a.out
1
1213
Ooops. If the file has no end-of-record markers, gfortran-compiled program does not read anything! ifx-compiled executable - still just the first value.
To me, it does not make any sense and is IMHO at odds with the very first statement of the section 12.3.3.4 Stream access of F2018 standard:
1 Stream access is a method of accessing the file storage units (12.3.5) of an external stream file.
Elsewhere (12.3.5) it is said:
It is recommended that the file storage unit be an 8-bit octet where this choice is practical.
so I guess it is not bad idea to understand stream access as byte-by-byte access.