Google Groups Home
Help | Sign in
Avoiding Labels
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
mich...@athenavisual.com  
View profile
 More options Jun 3 2007, 12:29 pm
Newsgroups: comp.lang.fortran
From: mich...@athenavisual.com
Date: Sun, 03 Jun 2007 09:29:42 -0700
Local: Sun, Jun 3 2007 12:29 pm
Subject: Avoiding Labels
Can this code be rewritten to avoid the label:

                open(unit=3, file='myData.txt')
                m=1
        do while (.true.)
         read(3,end=10,fmt=*)x(m),y1(m)
         m=m+1
        enddo
10             close(unit=3)

Thanks
Michael


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
highegg  
View profile
 More options Jun 3 2007, 12:46 pm
Newsgroups: comp.lang.fortran
From: highegg <high...@gmail.com>
Date: Sun, 03 Jun 2007 16:46:39 -0000
Local: Sun, Jun 3 2007 12:46 pm
Subject: Re: Avoiding Labels
On Jun 3, 6:29 pm, mich...@athenavisual.com wrote:

> Can this code be rewritten to avoid the label:

>                 open(unit=3, file='myData.txt')
>                 m=1
>         do while (.true.)
>          read(3,end=10,fmt=*)x(m),y1(m)
>          m=m+1
>         enddo
> 10             close(unit=3)

> Thanks
> Michael

integer:: ios
...
open(unit=3, file='myData.txt')
m = 1
do
  read(3,fmt=*,iostat=ios) x(m),y1(m)
  if (ios < 0) exit
  m = m + 1
end do
close(unit=3)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mich...@athenavisual.com  
View profile
 More options Jun 3 2007, 12:58 pm
Newsgroups: comp.lang.fortran
From: mich...@athenavisual.com
Date: Sun, 03 Jun 2007 09:58:26 -0700
Local: Sun, Jun 3 2007 12:58 pm
Subject: Re: Avoiding Labels
On Jun 3, 11:46 am, highegg <high...@gmail.com> wrote:

Thanks much
I appreciate the quick response

Michael


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Maine  
View profile
 More options Jun 3 2007, 2:56 pm
Newsgroups: comp.lang.fortran
From: nos...@see.signature (Richard Maine)
Date: Sun, 3 Jun 2007 11:56:45 -0700
Local: Sun, Jun 3 2007 2:56 pm
Subject: Re: Avoiding Labels

highegg <high...@gmail.com> wrote:
> integer:: ios
> ...
> open(unit=3, file='myData.txt')
> m = 1
> do
>   read(3,fmt=*,iostat=ios) x(m),y1(m)
>   if (ios < 0) exit
>   m = m + 1
> end do
> close(unit=3)

While that is a pretty literal translation of teh surface of the
original code, I'd advise against doing it quite that way. I would
advocate at least changing the test to ios/=0 instead of ios<0.

With the ios<0 test, the code continues trying to read after
encountering an error. Reading after an error would violate the
standard. Worse, however, it has a good chance of becomming an infinite
loop, always reporting an error and never an eof.

Better might be to output an error message and stop if ios>0. That
probably more accurately mimics what the original code would do in that
case.

While I'm on the subject of making suggestions for this code, I'd
suggest a dimension limit check on any I/O loop like this. That applies
to the original code as well as the modified version.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Louis Krupp  
View profile
 More options Jun 3 2007, 3:01 pm
Newsgroups: comp.lang.fortran
From: Louis Krupp <lkr...@pssw.nospam.com.invalid>
Date: Sun, 03 Jun 2007 13:01:42 -0600
Local: Sun, Jun 3 2007 3:01 pm
Subject: Re: Avoiding Labels

As far as I know, this won't handle the case where the read encounters
an error instead of an end-of-file.  You'd have to test for ios > 0 and
recover from the error or terminate the program.

I don't know if iostat values are portable.  If that's an issue, and
input errors are to be treated as catastrophic, then the 'end=<label>'
specifier might be the easiest (if not the most elegant) way of doing this.

(I assume the code checks for 'm' being out of bounds for 'x' or 'y1'.)

Louis


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Maine  
View profile
 More options Jun 3 2007, 3:26 pm
Newsgroups: comp.lang.fortran
From: nos...@see.signature (Richard Maine)
Date: Sun, 3 Jun 2007 12:26:07 -0700
Local: Sun, Jun 3 2007 3:26 pm
Subject: Re: Avoiding Labels

Louis Krupp <lkr...@pssw.nospam.com.invalid> wrote:
> I don't know if iostat values are portable.

The distinction between 0, <0, and >0 is standard and portable.
Otherwise, specific values are not portable (although f2003 adds
portable way to tell what the eof and eor values are).

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arjen Markus  
View profile
 More options Jun 4 2007, 3:01 am
Newsgroups: comp.lang.fortran
From: Arjen Markus <arjen.mar...@wldelft.nl>
Date: Mon, 04 Jun 2007 00:01:56 -0700
Local: Mon, Jun 4 2007 3:01 am
Subject: Re: Avoiding Labels
On 3 jun, 20:56, nos...@see.signature (Richard Maine) wrote:

> highegg <high...@gmail.com> wrote:
> > integer:: ios
> > ...
> > open(unit=3, file='myData.txt')
> > m = 1
> > do
> >   read(3,fmt=*,iostat=ios) x(m),y1(m)
> >   if (ios < 0) exit
> >   m = m + 1
> > end do
> > close(unit=3)

[...]

> While I'm on the subject of making suggestions for this code, I'd
> suggest a dimension limit check on any I/O loop like this. That applies
> to the original code as well as the modified version.

As the OP is not using a dynamically allocated array, the
check for the dimension limit is very easy to do:

open(unit=3, file='myData.txt')

do m = 1,size(x)
    read(3,fmt=*,iostat=ios) x(m),y1(m)
    if (ios < 0) exit
    if (ios > 0) then
        write(*,*) 'Error reading data pair ',m
        stop
   endif
end do
close(unit=3)

Regards,

Arjen


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Maine  
View profile
 More options Jun 4 2007, 10:56 am
Newsgroups: comp.lang.fortran
From: nos...@see.signature (Richard Maine)
Date: Mon, 4 Jun 2007 07:56:16 -0700
Local: Mon, Jun 4 2007 10:56 am
Subject: Re: Avoiding Labels

Arjen Markus <arjen.mar...@wldelft.nl> wrote:
> As the OP is not using a dynamically allocated array, the
> check for the dimension limit is very easy to do:
...
> do m = 1,size(x)
>     read(3,fmt=*,iostat=ios) x(m),y1(m)
>     if (ios < 0) exit
>     if (ios > 0) then
>         write(*,*) 'Error reading data pair ',m
>         stop
>    endif
> end do

Nothing unique to dynamically sized arrays there. It does assume that
the array size is set before this loop, as opposed to being adjusted to
accomdate the data being read; I suppose that's probably what you were
saying.

This simple method does avoid indexing the array out of bounds. However,
it does so by just ignoring any further data. That might be acceptable,
depending on the application. If you want to generate an error message
for that condition, it takes a little more work. In particular, you tend
to need to read into a temporary variable instead of directly into the
arrays because you need to try one read past the end of the array in
order to get the boundary condition right.

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arjen Markus  
View profile
 More options Jun 4 2007, 3:29 pm
Newsgroups: comp.lang.fortran
From: Arjen Markus <arjen.mar...@wldelft.nl>
Date: Mon, 04 Jun 2007 12:29:39 -0700
Local: Mon, Jun 4 2007 3:29 pm
Subject: Re: Avoiding Labels
On 4 jun, 16:56, nos...@see.signature (Richard Maine) wrote:

Hm, yes, that is true - with "dynamically allocated arrays", I was
thinking
of arrays that get expanded during the loop to allow the new data to
be
stored.

Detecting extra data could be done by using a bound like "size(x)+1"
and
storing the data only if the loop index is lower than that. The main
advantage above the do loop without any condition is that you can not
go wrong with initialising or incrementing the index.

Regards,

Arjen


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google