home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 108_01 / chcnt.c < prev    next >
Text File  |  1985-11-14  |  3KB  |  124 lines

  1.  
  2. /*
  3.     This program counts the frequency of each ASCII character
  4.         (0-127) in the list of files given as arguments, 
  5.     and prints the total number of characters in the files.
  6.     If the total number of characters exceeds 65535 (the
  7.     maximum value for "unsigned" integers in C), then the
  8.     wrong total will be printed. Oh well.
  9.     CHCNT.C has been included mainly as an example of
  10.     buffered file I/O. See TELNET.C for another example.
  11.  
  12.         written by L. Zolman   12/21/78 (Season's Greetings!!)  */
  13.  
  14. unsigned table[255];
  15. char ibuf[128];
  16.  
  17. char *ascy;
  18.  
  19. char *badfile;  /* message on error from "open" */
  20. char *readerr; /* message on read error */
  21.  
  22. main (argc,argv)
  23. char **argv;
  24. int argc;
  25. {
  26.         int n;
  27.         int fi;
  28.         int i;
  29.     ascy = "     ";
  30.         badfile = "chcnt: cannot  open "; 
  31.         readerr = "chcnt: read error from  ";
  32.     for (i=0; i<256; i++) table[i]=0;
  33.  
  34.         if (argc-- == 1) {
  35.                 printf("I need some filenames for this! \n");
  36.                 exit (0);
  37.          }
  38.         while (argc--) {
  39.                 if ((fi=open(*++argv,0)) < 0) {
  40.                         error (*argv,1);
  41.                         continue;
  42.                  }
  43.                 while ((n=read(fi,ibuf,1)) > 0)
  44.                         for (i=0; i<128; i++) table[ibuf[i]]++;
  45.  
  46.         if (n == -1) error(*argv,2);
  47.         close(fi);
  48.          }
  49.         display();
  50. }
  51.  
  52.  
  53. error(name,code)
  54. char *name;
  55. int code;
  56. {
  57.         if (code==1) printf("%s  ",badfile);
  58.         if (code==2) printf("%s  ",readerr);
  59.         printf("%s\n\n",name);
  60. }
  61.  
  62. display()
  63. {
  64.         int i;
  65.         int j;
  66.         int k;
  67.         char *cnvt();
  68.         unsigned count;
  69.     count = 0;
  70.  
  71.         printf("\n\n");
  72.         for (i=1; i<=8; i++) printf ("ch cnt    ");
  73.         printf("\n");
  74.         for (i=1; i<=8; i++) printf ("-- ---    ");
  75.         printf("\n\n");
  76.  
  77.         for (i=0; i<=15; i++) {
  78.     for (j=i,k=0; k<8; k++,j+=16) {
  79.                         count += table[j];
  80.                         printf ("%3s: %3u ",cnvt(j), table[j]);
  81.                  }
  82.                 printf ("\n");
  83.          }
  84.  
  85.         printf ("\nTotal # of chars read = %u\n",count);
  86. }
  87.  
  88.  
  89. char *cnvt(byte)
  90. int byte;
  91. {
  92.         int c;
  93.         c=byte;
  94.         switch(c)
  95.         {
  96.                 case 0: return("NU");
  97.                 case 13: return ("CR");
  98.                 case 10: return ("LF");
  99.                 case 27: return ("ES");
  100.                 case 28: return ("FS");
  101.                 case 29: return ("GS");
  102.                 case 30: return ("RS");
  103.                 case 31: return ("US");
  104.                 case 32: return ("SP");
  105.                 case 8:  return ("BS");
  106.                 case 127:return ("DL");
  107.                 default:
  108.                         if (c>=1 && c<=26) {
  109.                                 ascy[0]='^';
  110.                                 ascy[1]=c+64;
  111.                                 ascy[2]='\0';
  112.                                 return(ascy);
  113.                          }
  114.                         ascy[0]=' ';
  115.                         ascy[1]=c;
  116.                         ascy[2]= '\0';
  117.                         return(ascy);
  118.         }
  119. }
  120.  
  121.  
  122.  together with C.CCC (for common system
  123.     subroutines) and any subordinate functions which
  124.     "main" may require (from perhaps ma