home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_06 / 1n06042a < prev    next >
Text File  |  1990-09-30  |  632b  |  36 lines

  1.  
  2. Listing 9
  3.  
  4. /*
  5.  * fgetid skips leading whitespace and reads an identifier
  6.  * from file f into string s.  At most n - 1 characters
  7.  * are copied to s, and a '\0' is appended.  fgetid returns
  8.  * the length of s, or EOF if end of file is detected.
  9.  */
  10. int fgetid(FILE *f, char *s, size_t n)
  11.     {
  12.     char *p = s;
  13.     int c;
  14.  
  15.     while (isspace(c = fgetc(f)))
  16.         ;
  17.     if (isalpha(c) || c == '_')
  18.         {
  19.         do
  20.             if (p - s + 1 < n)
  21.                 *p++ = c;
  22.         while (isalnum(c = fgetc(f)) || c == '_');
  23.         ungetc(c, f);
  24.         *p = '\0';
  25.         return p - s;
  26.         }
  27.     else if (c != EOF)
  28.         {
  29.         ungetc(c, f);
  30.         return 0;
  31.         }
  32.     else
  33.         return EOF;
  34.     }
  35.  
  36.