Implicit decimal points in floating-point reads

Lee E. Brotzman brotzman at nssdca.gsfc.nasa.gov
Tue May 21 07:19:56 AEST 1991


Howdy,

   I am writing some code that reads a file format that uses text headers to
describe fields in ASCII tables (the format is called FITS for Flexible Image
Transport System -- tables are included as an extension of the image format).
The fields are described with standard FORTRAN 77 input formats.
   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, I have tried
using scanf with an input format of "%5.3f", but the result is garbage (see
below).
   Kernighan and Ritchie (1988) seems vague on the subject and doesn't really
say whether implicit decimal points are allowed on input.  The Turbo C
Reference Guide discussion of the format string for scanf does not mention the
precision part of the string (".3" in my example above).  
   I have tried the snippet of code below on both my PC under Turbo C and
on a VAX/VMS system under VAX C.  Both return a 0 for the number of items read
and 0.0 for the floating point value.
   My question:  is it possible to read data that are formatted with implicit
decimal points in ANSI standard C without writing my own routine? I do not need
code for a work-around, I can write my own.  I am just wondering whether I have 
to.  Before anyone mentions it -- no, I can not change the input data to 
include the decimal point. 
   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.  :-)

-- Lee E. Brotzman                    Internet:  brotzman at nssdca.gsfc.nasa.gov
-- ST Systems Corp.                   SPAN:      NSSDCA::BROTZMAN
-- National Space Science Data Center BITNET:    ZMLEB at SCFVM

---------------------------< TEST.C >---------------------------------
#include <stdio.h>

main()
{  char a1[10] = "26208";
   float x = 0.0;
   int ifld = 0;
   ifld = sscanf(a1, "%5.3f", &x);
   printf("%s %f\n%s %i", "input string converted to: ", x,
            "number of parameters converted: ", ifld);
}
----------------------------------------------------------------------
-------------------------< TEST.OUT >---------------------------------
input string converted to:  0.000000
number of parameters converted:  0
----------------------------------------------------------------------



More information about the Comp.lang.c mailing list