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.
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 & ¬ 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.
In article <75dpn7F17gd1...@mid.individual.net>, 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)
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.
> 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 Pastore <past...@units.it> wrote: > michaelmetc...@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:
> which allows to detect the end-of-file condition only.
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