Colin Millar writes:
> I am having trouble reading in a record of real numbers from an ascii file
> to a real array of fixed maximum size.
> I do not fully
> understand the limitations / benefits of using list directed input to read
> ascii files. Is it that the list directed input only completes when each
> array element has had data read into it? Or if the no. of entries in the
> file record and the size of the array do not match, then does that
> constitute an input conversion error??
That doesn't constitute a conversion error, but its consequences might.
I think the critical piece you are missing is that list-directed input
is *NOT* restricted to reading a single record. If you tell it
to read an array of, say, 100 elements, then list-directed imput
will continue until it reads 100 values, or until it hits the end
of file (in which case you will get an end-of-file condition),
or until it hits a "/" character.
What is probably happening to you is that, not finding enough values
on the first record, the list-directed read is continuing to
subsequent records where it finds data that doesn't look like
reals. (Or perhaps it finds some records of appropriate-looking
data, but has in the process used up records that you expected
to read with later statements, throwing them off).
Solutions include
1. If you can manage to put a "/" (without the quotes) after
the qbasic-written data, then the read will do exactly
what you want. You could even put the "/" on a separate
line as long as you never actually have enough data to
fill the array. (If you have enough data, then the read
will stop without going on to the "/" line).
2. You could read the whole line into a character variable
using an "A" format. Then do an internal read from that
character variable using list-directed. (This is an f90
list, so I assume you are using an f90 compiler. Internal
list-directed I/O is non-standard in f77, but is standard
in f90).
You'll then get an end-of-file condition when the read hits
the end of the character variable (so be sure to use an
appropriate end= or iostat=).
Note that none of the above solutions will directly tell you how many
elements were read. You might be able to tell that by pre-filling
the array with some value known to be invalid (assuming that you
can come up with such a value).
--
Richard Maine | Good judgment comes from experience;
[log in to unmask] | experience comes from bad judgment.
| -- Mark Twain
|