For Programmers: Free Programming Magazines  


Home > Archive > Fortran > May 2004 > stuck on array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author stuck on array
Steve

2004-05-12, 9:09 pm

Hi,I want to read a file (data.dat with 650 lines) with the following
format

name51 00 39 22.09 +21 15 04.9
name65 00 40 49.00 +40 11 19.7
name56 00 45 04.92 +01 47 12.9
name28 00 48 22.53 +05 17 00.2
..... etc ......

into three string-array: one for the first, one for the second and one
for the third column.

I do it in the following way

integer i
character*20 name(1000),two(1000),three(1000)

..... bla bla ...

*******Open the catalog (data.dat)**************
open(11, file='data.dat', status='old')

*******Read and store data.dat into 3 arrays
do 33 i=1,650
read(11,'a,a,a') name(i),two(i),three(i)
33 continue
close(11)

I compile it without any apparent problems, but when I run the program
it dump after the open command and it returns the following message

startio: error in format
apparent state: unit 11 named data.dat
last format: a,a,a
lately reading sequential formatted external IO
Aborted

So, I'm doing something wrong, but I do not understand what ... (I'm
pretty new and probably my error is terrible, but I can not find it
:-(

Thanks for any suggestion!
Cheers,
T.
meek@skyway.usask.ca

2004-05-12, 9:09 pm

In a previous article, Steve.Morris@libero.it (Steve) wrote:
>Hi,I want to read a file (data.dat with 650 lines) with the following
>format
>
>name51 00 39 22.09 +21 15 04.9
>name65 00 40 49.00 +40 11 19.7
>name56 00 45 04.92 +01 47 12.9
>name28 00 48 22.53 +05 17 00.2
>.... etc ......
>
>into three string-array: one for the first, one for the second and one
>for the third column.
>
>I do it in the following way
>
>integer i
>character*20 name(1000),two(1000),three(1000)
>
>.... bla bla ...
>
>*******Open the catalog (data.dat)**************
> open(11, file='data.dat', status='old')
>
>*******Read and store data.dat into 3 arrays
> do 33 i=1,650
> read(11,'a,a,a') name(i),two(i),three(i)


try read(11,'(a,a,a)') ....

but better to have the actual sizes of string -- and get rid of the tabs

... or you could also try read(11,*)name(i),two(i),three(i) in the hopes

that the tab will act as a delimiter.

Chris
Gus Gassmann

2004-05-12, 9:09 pm

Steve wrote:

> Hi,I want to read a file (data.dat with 650 lines) with the following
> format
>
> name51 00 39 22.09 +21 15 04.9


1234567890123456789|1234567890123456789|
1234

Note that your field widths do not correspond to the definitions
of your arrays. I suspect that the read skips to the next record
for the third item. Better to use a format like 'a15,a15,a15'.

>
> name65 00 40 49.00 +40 11 19.7
> name56 00 45 04.92 +01 47 12.9
> name28 00 48 22.53 +05 17 00.2
> .... etc ......
>
> into three string-array: one for the first, one for the second and one
> for the third column.
>
> I do it in the following way
>
> integer i
> character*20 name(1000),two(1000),three(1000)
>
> .... bla bla ...
>
> *******Open the catalog (data.dat)**************
> open(11, file='data.dat', status='old')
>
> *******Read and store data.dat into 3 arrays
> do 33 i=1,650
> read(11,'a,a,a') name(i),two(i),three(i)
> 33 continue
> close(11)
>
> I compile it without any apparent problems, but when I run the program
> it dump after the open command and it returns the following message
>
> startio: error in format
> apparent state: unit 11 named data.dat
> last format: a,a,a
> lately reading sequential formatted external IO
> Aborted
>
> So, I'm doing something wrong, but I do not understand what ... (I'm
> pretty new and probably my error is terrible, but I can not find it
> :-(
>
> Thanks for any suggestion!
> Cheers,
> T.





Dan Kidger

2004-05-12, 9:09 pm

m@skyway.usask.ca wrote:
> In a previous article, Steve.Morris@libero.it (Steve) wrote:
>
>
>
> try read(11,'(a,a,a)') ....
>
> but better to have the actual sizes of string -- and get rid of the tabs
>
> .. or you could also try read(11,*)name(i),two(i),three(i) in the hopes
>
> that the tab will act as a delimiter.
>
> Chris


yes - all format strings must begin and end with brackets:
read(11,'(a,a,a)')

but also it seems like each of the 3 strings you want has embedded
single spaces and that they are then seperated by 5 or more spaces.
I would probably recommend read each line as a single string:
read(11,'(a)') buffer
and then loop over this string to pull ot whatever bits you need using
buffer(i:j) syntax. the Fortran index() function can help here.
If you finally want to turn the input data into numbers to do maths then
you can of course use syntax like:
read(buffer(i:j),'(f11.3)') myval


Daniel.







Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com