Home > Archive > Fortran > August 2007 > How read the data in one line in this case?
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 read the data in one line in this case?
|
|
| li.simula@gmail.com 2007-08-21, 7:08 pm |
| I have a line data 3.0 5.0 1 2 6 4 8... in file named 10.
I define a type (must do as this)
type :: tt
real(8) :: q1,q2,number
integer, pointer :: q3(:) ! because I don't known the "number" of 1
2 6 4 8...so here use allocatable.
end type
Then, I read
read(10,*) q1,q2,number
allocate(q3(number))
read(10,*) (q3(i), i=1,number) ! ****
However, ***read is the read on next line, not the line of 3.0 5.0 1 2
6 4 8... .
Any suggestions?
| |
| Richard Maine 2007-08-21, 7:08 pm |
| On Tue, 21 Aug 2007 08:53:30 -0700, li.simula@gmail.com wrote
(in article <1187711610.237683.8920@x40g2000prg.googlegroups.com> ):
> integer, pointer :: q3(:) ! because I don't known the "number" of 1
> 2 6 4 8...so here use allocatable.
As 2 side comments.
1. This is not an allocatable, as the comment implies. This is a pointer,
which is not the same thing. Although both pointers and allocatables use the
allocate statement, they have some important differences. You will get in
trouble if you ignore those differences.
2. If you have a compiler new enough to support the allocatable TR, then you
could actually use an allocatable here instead of a pointer. That has several
advantages and fits your described need better. But older compilers won't
allow it.
> However, ***read is the read on next line, not the line of 3.0 5.0 1 2
> 6 4 8... .
This is a pretty frequently asked question here. There are basically a few
simple answers (and some more complicated ones, but I think the simple ones
will do better here).
1. Use non-advancing I/O. That is a pretty direct answer. However, you can't
use that with list-directed, so you'd need to make things slightly more
complicated.
2. Read the whole line into a character string. Then do internal reads from
the character string. The main caveat is that you need a character string
long enough for the maximum possible line.
3. Speaking of knowing the maximum possible, don't forget the simple
possibility of reading into a fixed size temporary array dimensioned to be at
least as large as the maximum possible valid size. Then copy the used portion
of that array to the allocated one in the structure.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
________________________________________
________
Hogwasher, Premier News and Mail for OS X
http://www.asar.com/cgi-bin/product.../hogwasher.html
________________________________________
________
| |
| Steven G. Kargl 2007-08-21, 7:08 pm |
| In article <0001HW.C2F06FD00100E2BFB042494F@news.supernews.com>,
Richard Maine <nospam@see.signature> writes:
> On Tue, 21 Aug 2007 08:53:30 -0700, li.simula@gmail.com wrote
> (in article <1187711610.237683.8920@x40g2000prg.googlegroups.com> ):
>
> This is a pretty frequently asked question here. There are basically a few
> simple answers (and some more complicated ones, but I think the simple ones
> will do better here).
>
> 1. Use non-advancing I/O. That is a pretty direct answer. However, you can't
> use that with list-directed, so you'd need to make things slightly more
> complicated.
>
> 2. Read the whole line into a character string. Then do internal reads from
> the character string. The main caveat is that you need a character string
> long enough for the maximum possible line.
>
> 3. Speaking of knowing the maximum possible, don't forget the simple
> possibility of reading into a fixed size temporary array dimensioned to be at
> least as large as the maximum possible valid size. Then copy the used portion
> of that array to the allocated one in the structure.
4. In some cases you can use a REWIND statement.
read(10,*) q1, q2, number
allocate(q3(n))
rewind(10)
read(10,*) q1, q2, number, q3
Note this places you at the start of unit=10, so one of Richard's
items is probably preferable for a file with multiple lines of input.
--
Steve
http://troutmask.apl.washington.edu/~kargl/
| |
| li.simula@gmail.com 2007-08-21, 10:08 pm |
| Thanks all!
|
|
|
|
|