Code Comments
Programming Forum and web based access to our favorite programming groups.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?
Post Follow-up to this messageOn 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 severa l 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 a t least as large as the maximum possible valid size. Then copy the used portio n 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 ________________________________________ ________
Post Follow-up to this messageIn 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 one s > 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 fro m > 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 port ion > 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/
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.