home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / graphics-0.17 / dist-stat / getnum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-13  |  1.2 KB  |  61 lines

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/getnum.c,v 1.1 90/09/01 11:36:04 toy Exp $
  3.  * NAME
  4.  *    get_number - read a number, skipping over invalid characters
  5.  *
  6.  * SYNOPSIS
  7.  *    int get_number(fp, value)
  8.  *    FILE    *fp;
  9.  *    double    *value;
  10.  *
  11.  * DESCRIPTION
  12.  *    Read a floating point number from the specified file.  Any
  13.  *    unknown characters are silently ignored.
  14.  *
  15.  *    On return, EOF is returned for the end of file, 0 for some
  16.  *    unspecified error, and 1 if the number was read correctly.
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <ctype.h>
  21.  
  22. #ifndef    lint
  23. static char RCSID[] = "@(#) $Id: getnum.c,v 1.1 90/09/01 11:36:04 toy Exp $";
  24. #endif
  25.  
  26. #if    defined(__STDC__) || defined(__GNUC__)
  27. int
  28. get_number (FILE * fp, double *value)
  29. #else
  30. int
  31. get_number (fp, value)
  32.      FILE *fp;
  33.      double *value;
  34. #endif
  35. {
  36.   int ch;
  37.  
  38.   /*
  39.    * Skip over any characters that can't be part of a
  40.    * number
  41.    */
  42.  
  43.   do
  44.     {
  45.       ch = getc (fp);
  46.   } while ((ch != EOF) && (ch != '-') && (ch != '.') && (!isdigit (ch)));
  47.  
  48.   if (ch != EOF)
  49.     {
  50.  
  51.       /*
  52.        * We must have something that looks like the start of a number,
  53.        * so pretend it is.  If it's not, fscanf will tell us that.
  54.        */
  55.  
  56.       (void) ungetc (ch, fp);
  57.       ch = fscanf (fp, "%lf", value);
  58.     }
  59.   return ch;
  60. }
  61.