home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06106a < prev    next >
Text File  |  1990-12-12  |  1KB  |  42 lines

  1.  
  2.  
  3. //wcount.cpp
  4. //counts lines, words, non-whitespace characters and bytes in
  5. //ASCII files
  6.  
  7. #include <stdio.h>
  8. #include <stream.hpp>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11.  
  12. main(int argc, char *argv[])
  13. {
  14.    unsigned char ch, lastch;
  15.    unsigned long bytecount = 0, charcount = 0, 
  16.                                wordcount = 0, linecount = 0;
  17.    filebuf file1;
  18.    if (file1.open(argv[1],input) == 0) exit(1);
  19.    istream input_file(&file1);
  20.  
  21.    for (;;){
  22.       input_file.get(ch);
  23.       if (ch == '\n') linecount++;
  24.       if (bytecount == 0 && !isgraph(ch)) lastch = ch;
  25.       if (!isspace(lastch) && !iscntrl(lastch) && (isspace(ch)
  26.               || iscntrl(ch) || input_file.eof())) wordcount++;
  27.       if (input_file.eof()) break;  
  28.       if (!isspace(ch) && !iscntrl(ch)) charcount++;
  29.       bytecount++;      
  30.       lastch = ch;
  31.       }
  32.    if (linecount == 0) linecount = 1;
  33.  
  34.    cout << "File " << argv[1] << " contains:\n"                                     
  35.         << dec(linecount,21) << " line(s)\n"                    
  36.         << dec(wordcount,21) << " words\n" << dec(charcount,21) 
  37.         << " characters excluding whitespace\n" 
  38.         << dec(bytecount,21) << " bytes\n";
  39. }
  40.  
  41.  
  42.