Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Why is last record READ twice?

algebra
Beginner
553 Views
I opened a (binary) file for Unformatted Direct Read (I'm not using BINARY), and everything is fine, except that the last record is read twice. Why is that? I'm pretty sure my datafile does not have two identical records at the end (I checked with a Hex editor). My code below:

do while (errcode == 0)
read (10, rec=recnum, iostat=errcode) a,b,c
recnum = recnum + 1
print '(I6,I5,I1)', a,b,c
end do

The last record, and only the last record, is printed twice. Why?
0 Kudos
2 Replies
Steven_L_Intel1
Employee
553 Views
It's not read twice (per your title), but it is printed twice, because you don't test errcode before printing.
0 Kudos
algebra
Beginner
553 Views
Ahhhh, I see! I'm just so used to that loop idiom (from other languages) to read from files, and to count on it to stop at EOF. Of course, since we're reading fixed records, the EOF won't be hit until *after* the last record. Thanks Steve!
0 Kudos
Reply