Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How read the data in one line in this case?
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?


Report this thread to moderator Post Follow-up to this message
Old Post
li.simula@gmail.com
08-22-07 12:08 AM


Re: How read the data in one line in this case?
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 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
 ________________________________________
________


Report this thread to moderator Post Follow-up to this message
Old Post
Richard Maine
08-22-07 12:08 AM


Re: How read the data in one line in this case?
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 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/

Report this thread to moderator Post Follow-up to this message
Old Post
Steven G. Kargl
08-22-07 12:08 AM


Re: How read the data in one line in this case?
Thanks all!


Report this thread to moderator Post Follow-up to this message
Old Post
li.simula@gmail.com
08-22-07 03:08 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Fortran archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 02:50 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.