home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / WC.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  69 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4.    File wc.c - a sample word count program
  5.    Written and submitted to public domain by Jay Elkes
  6.    April, 1992
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12.  
  13. int main (int argc, char *argv[])
  14. {
  15.       FILE *infileptr;
  16.       char infile[80];
  17.  
  18.       long int nl = 0;
  19.       long int nc = 0;
  20.       long int nw = 0;
  21.  
  22.       int state = 0;
  23.       const int  NEWLINE = '\n';
  24.       int  c;
  25.  
  26. /*  The program name itself is the first command line argument so we
  27.     ignore it (argv[0]) when showing user entered parameters. */
  28.  
  29.       switch (argc - 1)
  30.       {
  31.       case (0):
  32.             printf("no parameters\n");
  33.             return 12;
  34.       case (1):
  35.             break;
  36.       default:
  37.             printf("too many parameters\n");
  38.             return 12;
  39.       }
  40.  
  41.       strcpy(infile,argv[1]);
  42.  
  43.       infileptr = fopen(infile,"rb");
  44.       if (infileptr == NULL)
  45.       {
  46.             printf("Cannot open %s\n",infile);
  47.             return 12;
  48.       }
  49.  
  50.       while ((c = getc(infileptr)) != EOF)
  51.       {
  52.             ++nc;
  53.             if (c == NEWLINE)
  54.                   ++nl;
  55.             if (isspace(c))
  56.                   state = 0;
  57.             else if (state == 0)
  58.             {
  59.                   state = 1;
  60.                   ++nw;
  61.             }
  62.       }
  63.  
  64.       /* Final Housekeeping */
  65.  
  66.       printf("%ld Lines, %ld Words, %ld Characters", nl, nw, nc);
  67.       return 0;
  68. }
  69.