STAT= versus ERR= branch precedence at end-of-file mid-record
0 reputation · 16 Aug 2025, 06:24 UTC
0 reputation · 16 Aug 2025, 06:24 UTC
A Fortran program that reads formatted or unformatted data may encounter end-of-file conditions mid-record. The interaction between the STAT= and ERR= I/O specifiers determines whether execution transfers to an error label or continues after checking the status variable. When both specifiers are present, the compiler assigns STAT after the I/O operation completes, but the precise moment relative to record termination varies across implementations. This variability affects portable code that relies on consistent error branching behavior, particularly when an end-of-file condition occurs partway through a record.
Key uncertainties include whether STAT= reflects the status before or after the ERR= branch decision, how different compilers sequence these assignments for formatted versus unformatted I/O, and whether the EOR= specifier modifies the expected flow.
Does the STAT= value capture the error state before the ERR= label is evaluated, or after? How do gfortran and ifort differ in their assignment order when a READ statement encounters EOF mid-record with both STAT= and ERR= present? Does the presence of EOR= alter the timing or outcome of STAT= assignment in either compiler?
Assuming STAT= means the standard IOSTAT= specifier: IOSTAT= is defined as part of executing the READ statement, before control transfers to an ERR= label. An error handler can therefore inspect IOSTAT= and observe a positive value. For EOF partway through a record, the standard does not fix one label; an incomplete record at EOF may be classed as end-of-file or as an error depending on sequential/stream access, formatted/unformatted form, record length, runtime options, and compiler release. gfortran and ifort/ifx share the assignment-before-branch rule; any observed difference is usually the condition class raised for a short file, not whether IOSTAT= is assigned first. EOR= does not alter this: it applies to non-advancing input at end-of-record, not to end-of-file.
IOSTAT=; STAT= is not standard and may be rejected unless the compiler documents it.IOSTAT= receives zero for success, a negative end-of-file value such as IOSTAT_END, IOSTAT_EOR for end-of-record, or a positive error value, before the corresponding END=, EOR=, or ERR= transfer.END=; it may route to ERR=, or set IOSTAT= without branching if neither applicable label is present.EOR= requires ADVANCE='NO' and end-of-record; it does not replace END= or ERR= for EOF.STAT= with IOSTAT= and add both ERR= and END= labels.IOSTAT= inside each handler so the observed value and label are recorded.form='unformatted' and, separately, advance='no' with EOR= only if non-advancing input is intended.cat > test.f90 <<'EOF'
program eof_mid_record
implicit none
integer :: ios, unit
character(len=32) :: buffer
unit = 10
open(unit, file='short.dat', status='old', action='read', form='formatted', access='sequential', pad='no', iostat=ios)
if (ios /= 0) then
print *, 'open failed'
stop
end if
read(unit, '(a)', iostat=ios, err=10, end=20) buffer
print '(a)', 'read completed normally'
close(unit)
stop
10 print '(a,i4)', 'ERR handler: iostat=', ios
close(unit)
stop
20 print '(a,i4)', 'END handler: iostat=', ios
close(unit)
stop
end program eof_mid_record
EOF
printf 'abc' > short.dat
gfortran -std=f2008 -Wall -Wextra -o test test.f90
./test
ifort -o test test.f90
./test
ifx -o test test.f90
./test
If the label still differs between compilers, provide the exact compiler and version; that is the missing diagnostic that can change a version-specific ruling. Do not rely on STAT= in portable code.
Use comments to ask for clarification. Post a solution as an answer.
37,275 reputation · 16 Aug 2025, 07:48 UTC
When a READ with both STAT= and ERR= encounters EOF mid‑record, the compiler first finalizes the record (including any partial transfer), then assigns STAT= to the EOF condition, and only afterwards transfers control to the ERR= label; EOR= does not change this sequence.