Re: newbie question for reading input file



www <www@xxxxxxxxxx> wrote:

READ(IN,900) ANAME,ELEV,ALAT,PRC,ODS,OSS,SS,UPS,RDCOS,AEC
900 FORMAT(5A4,2F5.0,1X,A4,2X,A3,2X,A3,1X,A4,4X,A4,A2,1X,A4,1X,A4)
WRITE(*,*) 'ANAME=',ANAME
....
<output>
ANAME= 1.0749562E+10 2.1609608E+11 160.2706 1.1015335E+13
1.3579762E-19
....
The whole program runs successfully. But I really have several questions:
1. ANAME should be "S. PRAIRIE CRK UPR", why output is not? Similar
question with variable SS, which should be "SUMS".

You don't show the declarations. Declarations are *VERY* important. It
is fairly common for people to seek help for problems that turn out to
be in the declarations, which aren't shown. That appears to be the case
here. Fortunately, this is a simple enough case that I can guess the
important part. But for future reference, be aware that declarations are
important.

ANAME and SS are of type real (either by default or explicit
declaration). That's why they print out as real values. You should
ideally declare them to be character strings instead.

This code uses a mixture of very old and newer features. The very old
feature is using a real variable to store character values. That was the
only way to do character data in Fortran 66 and earlier; there wasn't a
character type. It became nonstandard in Fortran 77 (30 years ago), even
though most f77 compilers, and many later ones as well, continued to
support the old option. Using an "A" (character) format edit descriptor
with a real variable is technically nonstandard.

Anyway, even if you are using a compiler that supports the old Hollerith
usage (that's what putting character data in noncharacter variables is
called), that won't go well with using the newer feature of
list-directed output formatting. (That's the second * in your write
(*,*)). List-directed output formatting just sees a real variable, so it
uses formatting appropriate for reals - not characters. If you are using
Hollerith, you can't output it meaningfully with list-directed output.
You have to use an explicit format with an appropriate "A" edit
descriptor, just like you did for the input. Using character variables
is better, though.

2. READ(IN, 900) has 10 variables, but 900 FORMAT(11 variables here).
Does that mean last one ("1x, A4") is ignored?

First, a correction. The FORMAT doesn't have anything that corresponds
directly to variables. It has edit descriptors that define fields that
correspond to individual data items. For example, your first edit
descriptor is 5A4. That defines 5 fields. It does *NOT* necessarily
correspond to a single variable. Apparently your ANAME variable is
dimensioned with size 5, so it uses all 5 fields. But that isn't so in
general. The 5 fields could correspond to 5 separate variables.

So, counting fields defined by your FORMAT, I see 15 fields, 5 from the
5a4, 2 from the 2f10.5, and 8 more. The X edit descriptrs don't count as
defining field; they just skip columns. I see 15 values to go with
these 15 fields. You have 10 variables; one is an array of 5 elements,
one an array of 2 elements, and 8 are scalars (at least that's what I
deduce, though you don't show the declarations). That seems to match.

Hmm. Where did you get a count of 11 of anything from. I was assuming
that you were just calling the 5a4 and 2f10.5 one thing each, but doing
that I only come up with 10. Did you just miscount? (Easy to do).

Anyway, the answer, after rephrasing in the right terms, is yes, that
unused data edit descriptors at the end of the format are ignored, even
though it doesn't look like you have that situation.

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



Relevant Pages