For Programmers: Free Programming Magazines  


Home > Archive > Fortran > May 2005 > How can I read data from ".dat" file to an 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 How can I read data from ".dat" file to an array?
Ying Zu

2005-05-13, 4:10 pm

Maybe this question was even asked to death but I'm eager to know the
answer to finish my graduate project which oblige me to handle fortran
in few days.:)

the case is, I have a file named 'data.dat' which was make from former
codes with WRITE in 'FORMAT(1X,7E13.5)'.
So, there was six columns of datas lying in the file, what I want to
do is read the first and the second columns to two specific arrays,
but I can't figure out how to make an effective program to achieve
this.

Thanks for giving your answer. :)
David Flower

2005-05-13, 4:10 pm

DO I = 1, whatever
READ ( 42, '(1X,2E13.5)' ) X(I), Y(I)
END DO

PS There's no such word as 'datas'; 'data' is the plural of 'datum'

Dick Russell

2005-05-13, 4:10 pm

There is no particular need to modify the original format string given;
with just two I/O list items in the READ, only the first two of the 7
repetitions of E13.5 get used. On the other hand, if you had combined
the DO with the READ, then the repetition count does become important:

READ (42, '(1X,2E13,.5)' ) (X(I), Y(I), I=1,whatever)

The original poster should note that the "42" after the READ is the
unit number assigned to the file when the OPEN statement was executed.
Also, he should understand the interaction between the format string
and the I/O list; for each item in the READ, the Fortran runtime
routines that handle the READ use the next specification in the format
string; when it hits the right parenthesis, it gets a new line from the
file and repeats interpreting from the left parentheses of the format
string. This is a simplification of the process, but in the interests
of time, it should be enough for now. Format strings can be quite
complicated, but they can provide a lot of power for extracting
information from files or for arranging output from WRITE statements. A
good text on the Fortran language will help you. Many web sites have
explanations of the elements of the language online. Good luck with
your graduate project.

Dick Russell

2005-05-13, 4:10 pm

There is no particular need to modify the original format string given;
with just two I/O list items in the READ, only the first two of the 7
repetitions of E13.5 get used. On the other hand, if you had combined
the DO with the READ, then the repetition count does become important:

READ (42, '(1X,2E13,.5)' ) (X(I), Y(I), I=1,whatever)

The original poster should note that the "42" after the READ is the
unit number assigned to the file when the OPEN statement was executed.
Also, he should understand the interaction between the format string
and the I/O list; for each item in the READ, the Fortran runtime
routines that handle the READ use the next specification in the format
string; when it hits the right parenthesis, it gets a new line from the
file and repeats interpreting from the left parentheses of the format
string. This is a simplification of the process, but in the interests
of time, it should be enough for now. Format strings can be quite
complicated, but they can provide a lot of power for extracting
information from files or for arranging output from WRITE statements. A
good text on the Fortran language will help you. Many web sites have
explanations of the elements of the language online. Good luck with
your graduate project.

beliavsky@aol.com

2005-05-13, 4:10 pm

David Flower wrote:
> DO I = 1, whatever
> READ ( 42, '(1X,2E13.5)' ) X(I), Y(I)
> END DO


This is correct, but I recommend a list-directed read,

READ (42,*) X(I),Y(I)

because it depends less on the precise format of the input file.

Kevin G. Rhoads

2005-05-13, 4:10 pm

>PS There's no such word as 'datas'; 'data' is the plural of 'datum'

Yes, "data" is the plural for "datum", but "datas" is also a word, a neologism
promulgated by clueless bureaucrats and PHBs. I don't envy the French their
language police usually, but occasionally ...
David Flower

2005-05-13, 4:10 pm

True; however the code I gave has the advantage of being easily
modified to read in any subset of the line; for example:

READ ( 42, '(14X,E13.5,13X,E13.5,13X,E13.5)' ) X(I), Y(I), Z(I)

would read columns 2, 4 and 6

Now where did that number 42 come from ?

meek@skyway.usask.ca

2005-05-13, 4:10 pm

In a previous article, "David Flower" <DavJFlower@AOL.COM> wrote:
>True; however the code I gave has the advantage of being easily
>modified to read in any subset of the line; for example:
>
>READ ( 42, '(14X,E13.5,13X,E13.5,13X,E13.5)' ) X(I), Y(I), Z(I)
>
>would read columns 2, 4 and 6
>
>Now where did that number 42 come from ?
>

Isn't that the answer to the world, universe, and everything ?
Chris
Michel OLAGNON

2005-05-13, 4:10 pm



beliavsky@aol.com wrote:
> David Flower wrote:
>
>
>
> This is correct, but I recommend a list-directed read,
>
> READ (42,*) X(I),Y(I)
>
> because it depends less on the precise format of the input file.
>


When that precise format is known, better use it !

For instance, READ ( 42, '(1X,2E11.5)' ) X(I), Y(I)
is much superior to READ (42,*) X(I),Y(I) when the
input is
0.33333E+000.33333E+00

zuying@gmail.com

2005-05-18, 4:00 pm


Dick Russell wrote:
> There is no particular need to modify the original format string

given;
> with just two I/O list items in the READ, only the first two of the 7
> repetitions of E13.5 get used. On the other hand, if you had combined
> the DO with the READ, then the repetition count does become

important:
>
> READ (42, '(1X,2E13,.5)' ) (X(I), Y(I), I=1,whatever)
>
> The original poster should note that the "42" after the READ is the
> unit number assigned to the file when the OPEN statement was

executed.
> Also, he should understand the interaction between the format string
> and the I/O list; for each item in the READ, the Fortran runtime
> routines that handle the READ use the next specification in the

format
> string; when it hits the right parenthesis, it gets a new line from

the
> file and repeats interpreting from the left parentheses of the format
> string. This is a simplification of the process, but in the interests
> of time, it should be enough for now. Format strings can be quite
> complicated, but they can provide a lot of power for extracting
> information from files or for arranging output from WRITE statements.

A
> good text on the Fortran language will help you. Many web sites have
> explanations of the elements of the language online. Good luck with
> your graduate project.


Sponsored Links







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

Copyright 2009 codecomments.com