home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / WC.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  1KB  |  67 lines

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