Implicit decimal points in floating-point reads

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Tue May 21 20:16:50 AEST 1991


In article <5366 at dftsrv.gsfc.nasa.gov>, brotzman at nssdca.gsfc.nasa.gov (Lee E. Brotzman) writes:
>    The problem I am encountering is this:  FORTRAN allows input strings
> representing floating point values to have implicit decimal places, i.e.
> the string "26208" read with a format of F5.3 results in a value of 26.208.
> As far as I can tell there is no equivalent functionality in C

Scanf breaks out fields, then converts those fields to binary
exactly the way the strtod() function would.  There is no way at all
for you to say where the implicit decimal point would be.  "%5.3f" is
_not_ a valid scanf() format in C.

If you want to read a field of width W and then convert that to
floating point, use "%Wc" (where W is a literal integer, e.g. "%5c")
to read the characters, and then write your own function to parse them.

I have my own scanf() replacement kit which lets me read most Fortran
formats quite easily.

Something you might consider doing is picking up "f2c" from
research.att.com.  That contains an implementation of Fortran input
(and of course output) written in C.  The simplest thing for you to
do might be to use that library.

>    I'd be surprised that such an obviously useful bit of functionality
> that has existed for decades in FORTRAN isn't available in C, especially
> considering all of the other features packed into the scanf routine. 
> Please tell me that I'm being a bonehead and missing something obvious.  :-)

It may be obviously useful to you, but it's amazing how many C programmers
never missed it.  When I used Fortran, I always thought that implied
decimal points were a "feature" to let you squeeze one more column out of
a punched card, and designed my input formats so that they weren't needed.
It made checking the data so much easier.
-- 
There is no such thing as a balanced ecology; ecosystems are chaotic.



More information about the Comp.lang.c mailing list