Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29488 Discussions

I/O formatting - reading real values without decimal point

h_amini
Beginner
1,162 Views

My program scans a data file and reads real numbersvia internal readings. The numbers can be with or without E exponent. Also they may not have a decimal point. I solved it as bellow and would appreciate any comment.

Kind regards

Hamid

DOUBLE PRECISION XCOORD

CHARACTER*80, TEMPSTRING

TEMPSTRING = " 1E+03 "

! Remove leading blanks

TEMPSTRING = ADJUSTL(TEMPSTRING)

! Position of decimal point

IF (INDEX(TEMPSTRING,'.') .EQ. 0) THEN

I3 = INDEX(TEMPSTRING,' ') ! Position of next first blank

READ(TEMPSTRING, '(E.0)') XCOORD

ELSE

READ(TEMPSTRING, '(E)') XCOORD

END IF

PRINT *, "XCOORD", XCOORD

END

0 Kudos
3 Replies
Steven_L_Intel1
Employee
1,162 Views
I am not sure why you found it necessary to use a different format - The E format with .0 for the .d should suffice. Also, I see you are using a variable format expression extension (the ) - you could read from a substring of TEMPSTRING and not need the VFE.

However, if this meets your needs, then that's what really matters.
0 Kudos
h_amini
Beginner
1,162 Views
I am not sure why you found it necessary to use a different format - The E format with .0 for the .d should suffice. Also, I see you are using a variable format expression extension (the ) - you could read from a substring of TEMPSTRING and not need the VFE.

However, if this meets your needs, then that's what really matters.

Many thanks for this.

Using only the E format with .0 seems work but I am not completely sure. As I know the .d is the number of places after the decimal point (please correct me if I am mistaken) and I don't know if it can always be zero. That is why I used different formats.

I used VFE to specify the field width as they vary. Is it possible to read from a substring when the width is unknown?

Hamid

0 Kudos
Steven_L_Intel1
Employee
1,162 Views
The .d part of an E format says how many fraction digits to assume if a decimal point is not present.

It is possible to read from a substring when the width is unknown, though you really have to take advantage of another extension to do so, unless you're willing to write a format string. The Intel compiler will shorten the format width if the input field is short. Maybe using the VFE isn't so bad after all!
0 Kudos
Reply