Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

EOF idiom?

895 views
Skip to first unread message

Bruce Bowler

unread,
Apr 24, 2009, 8:30:31 AM4/24/09
to
What's the (standard conforming) Fortran 95 way to deal with

10 read (blah,end=20)
process stuff
goto 10
20 close (blah)

In many other languages I'd do something like

read (blah)
do while not eof(blah)
process stuff
read (blah)
end do

but there doesn't seem to be an eof function in Fortran

Thanks
Bruce

feenberg

unread,
Apr 24, 2009, 9:01:34 AM4/24/09
to
On Apr 24, 8:30 am, Bruce Bowler <bbow...@bigelow.org> wrote:
> What's the (standard conforming) Fortran 95 way to deal with
>
> 10 read (blah,end=20)
>    process stuff
>    goto 10
> 20 close (blah)

That's the way I do it.

>
> In many other languages I'd do something like
>
> read (blah)
> do while not eof(blah)
>   process stuff
>   read (blah)
> end do
>
> but there doesn't seem to be an eof function in Fortran

The advantage of end= over eof(unit) is that the read statement
doesn't need to be repeated.
I'd say that was a real advantage in terms of "edit sensitivity". I
agree the statement numbers are
primitive.

I expect that somehow the INQUIRE statement can be used to create and
eof function (perhaps
with the POSITION qualifier equal to "APPEND"), but I haven't tried
that.

Daniel Feenberg

>
> Thanks
> Bruce

michael...@compuserve.com

unread,
Apr 24, 2009, 9:01:35 AM4/24/09
to
integer :: stat
do
read(blah, iostat=stat)
if(stat /= 0) exit
process stuff
end do
close(blah)

This catches all conditions, not just EOF. If you want only that, then
end=label can be used, but that requires, yes, a label.

Regards,

Mike Metcalf

Jugoslav Dujic

unread,
Apr 24, 2009, 9:02:53 AM4/24/09
to
Bruce Bowler wrote:
> What's the (standard conforming) Fortran 95 way to deal with
>
> 10 read (blah,end=20)
> process stuff
> goto 10
> 20 close (blah)

Well, the above is also a Fortran-95 way, but if you want to
avoid that abominable disgusting considered harmful
<small>just kidding</small> goto, you can do:

do
read(blah, end=20)
process stuff
end do
20 close(blah)

and in order to avoid that abominable disgusting label, you
can do:

Reading_loop: &
do
read(blah, iostat=ierr)
if (ierr<0) then
exit Reading_loop
else if (ierr>0) then
stop "Sorry, I'm afraid that the file format is &
&not what I expected"
end if
process stuff
end do Reading_loop !Named do-loop for extra points in fanciness
close(20)

--
Jugoslav
www.xeffort.com
Please reply to the newsgroup.
You can find my real e-mail on my home page above.

Ron Shepard

unread,
Apr 24, 2009, 3:36:42 PM4/24/09
to
In article <75dpn7F...@mid.individual.net>,
Bruce Bowler <bbo...@bigelow.org> wrote:

> What's the (standard conforming) Fortran 95 way to deal with
>
> 10 read (blah,end=20)
> process stuff
> goto 10
> 20 close (blah)

I think this approach would work in any version of fortran since f77,
including f95.

>
> In many other languages I'd do something like
>
> read (blah)
> do while not eof(blah)
> process stuff
> read (blah)
> end do

You could do the same thing in f95. Instead of eof(), you would check
the iostat value, but it is the same structure. The esthetic problem
with this is that you have two read statements while one would suffice

Or, if you don't like "do while" (and many programmers don't), you could
write it with a single read statement as

do
read(...,iostat=ioval)...
if(ioval .ne. 0) exit
process stuff
enddo

At least to my eye, that looks simpler and cleaner than your code above.
It doesn't do exactly the same thing (it catches both eof and error
conditions), but you could generalize the test a little if appropriate.

$.02 -Ron Shepard

Giorgio Pastore

unread,
Apr 24, 2009, 4:41:39 PM4/24/09
to
michael...@compuserve.com wrote:
...

> read(blah, iostat=stat)
> if(stat /= 0) exit
...

> This catches all conditions, not just EOF. If you want only that, then
> end=label can be used, but that requires, yes, a label.

Or, if the compiler is 2003 compliant, by using iso_fortran_env
intrinsic module, one could just write:

read(blah, iostat=stat)
if(stat==iostat_end)exit

which allows to detect the end-of-file condition only.

Giorgio

Richard Maine

unread,
Apr 24, 2009, 5:25:23 PM4/24/09
to
Giorgio Pastore <pas...@units.it> wrote:

Even without that constant, you could just check for iostat<0, as the
only other possible iostat value <0 is for end-of-record, which applies
only to non-advancing I/O.

However, in either case, note that I strongly recomend against having an
iostat specifier and then ignoring some of its possible values. If you
hit an error, you will just cover up its immediate symptoms, turning
them into more confusing later ones. Odds are that you don't want to do
the same thing for an error as for an end-of-file, but don't just ignore
it completely.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain

0 new messages