Article View: comp.lang.fortran
Article #4505Re: Reading a real from a character substring
From: Bill Cornette
Date: Mon, 25 Jan 1993 21:49
Date: Mon, 25 Jan 1993 21:49
95 lines
6329 bytes
6329 bytes
Conor McMenamin (mppu3@syma.sussex.ac.uk) wrote:
: Conor McMenamin (me) wrote:
: : Hi,
: : I need to find as standard as possible a routine to read a real
: : number from a character variable (well actually a substring of a
: : character variable but that's easy to work round...). However my problem
: : is that I do not know the format of the number which I am reading in. I
: : have tried to "READ (string(a:b),*) real" but my compiler tells me that
: : internal I/O must be formatted....anybody got any ideas how to do this a
: : standard way? Or is my compiler broken?
: :
: : Cheers,
: : Conor
:
: Thanks for the multitude of replies I recieved. All were accuarte as far
: as I could make out, but the winner (for exactly solving my problem in
: minimum code lines) is Paul Schlyter who wrote:
:
: Paul>Try:
: Paul>
: Paul> character fmt(6)
: Paul>
: Paul> ........
: Paul> write( fmt, '(''(F'',I1,''.0)'')' ) b-a+1
: Paul> read( string(a:b), fmt ) real
: Paul>
: Paul> Format strings can be created at runtime.....
: Paul> Note that the .0 in the created F format only matters at output,
: Paul> not at input.
: Paul>
If you want to have the format calculated try the FUNCTION:
REAL FUNCTION GETVAR(VARIAB) gva 0010
C Created on: Wed Nov 18 15:56:43 1992 gva 0020
C Created by: Dr. William M. Cornette gva 0030
C gva 0040
C**** This FUNCTION reads a REAL variable contained in free gva 0050
C format in the CHARACTER string VARIAB. gva 0060
C gva 0070
C VARIAB - CHARACTER*(*) Variable - Input string gva 0080
C gva 0090
C**** INTRINSIC and EXTERNAL Declarations gva 0100
C gva 0110
INTRINSIC LEN gva 0120
C gva 0130
C**** Argument Declarations gva 0140
C gva 0150
CHARACTER*(*) VARIAB gva 0160
C gva 0170
C**** Local Variable Declarations gva 0180
C gva 0190
INTEGER I,ICNT,LENSTR,IOS gva 0200
REAL DEFALT gva 0210
CHARACTER*7 FMTSTR gva 0220
C gva 0230
DATA DEFALT /-9999./ gva 0240
C gva 0250
LENSTR=LEN(VARIAB) gva 0260
ICNT=1 gva 0270
DO 101 I = LENSTR,1,-1 gva 0280
IF (VARIAB(I:I).NE.' ') THEN gva 0290
ICNT=I gva 0300
GO TO 102 gva 0310
END IF gva 0320
101 CONTINUE gva 0330
102 CONTINUE gva 0340
WRITE(UNIT=FMTSTR,FMT=1,ERR=104,IOSTAT=IOS) ICNT gva 0350
READ(UNIT=VARIAB(1:ICNT),FMT=FMTSTR,ERR=103,IOSTAT=IOS) GETVAR gva 0360
103 CONTINUE gva 0370
IF (IOS.NE.0) GETVAR=DEFALT gva 0380
C gva 0390
RETURN gva 0400
C gva 0410
104 CONTINUE gva 0420
WRITE(UNIT=*,FMT=2) ICNT,VARIAB(1:ICNT),IOS gva 0430
STOP 'Error No. 208' gva 0440
C gva 0450
1 FORMAT('(G',I2,'.0)') gva 0460
2 FORMAT(1X/1X,'WRITE Error in getvar. Calculation terminated.'/ gva 0470
+4X,'ICNT = ',I5,1X,A/ gva 0480
+4X,'IOSTAT = ',I5) gva 0490
END gva 0500
---------------------------------------------------------------------------
---------------------------------------------------------------------------
William M. Cornette Photon Research Associates, Inc.
(619) 455-9741 10350 N. Torrey Pines Road, Suite 300
FAX (619) 455-0658 La Jolla, California 92037
wmc@photon.com
---------------------------------------------------------------------------
T.A.N.S.T.A.A.F.L.
Technical Details
Message-ID:
<1993Jan25.214937.4491@photon.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!paladin.american.edu!howland.reston.ans.net!usc!sdd.hp.com!crash!photon.photon.com!wmcReferences:
<1993Jan19.080949.17968@syma.sussex.ac.uk>Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.fortran Path: arc1-ca.pugleaf.net!sparky!uunet!paladin.american.edu!howland.reston.ans.net!usc!sdd.hp.com!crash!photon.photon.com!wmc From: wmc@photon.com (Bill Cornette) Subject: Re: Reading a real from a character substring Message-ID: <1993Jan25.214937.4491@photon.com> Sender: usenet@photon.com (Usenet dummy users) Organization: Photon Research Associates, Inc. X-Newsreader: TIN [version 1.1 PL6] References: <1993Jan19.080949.17968@syma.sussex.ac.uk> Date: Mon, 25 Jan 1993 21:49:37 GMT Lines: 95