Listing 1

//wcount.cpp
//counts lines, words, non-whitespace characters and bytes in
//ASCII files

#include <stdio.h>
#include <stream.hpp>
#include <stdlib.h>
#include <ctype.h>

main(int argc, char *argv[])
{
   unsigned char ch, lastch;
   unsigned long bytecount = 0, charcount = 0,
                 wordcount = 0, linecount = 0;
   filebuf file1;
   if (file1.open(argv[1],input) == 0) exit(1);
   istream input_file(&file1);

   for (;;){
      input_file.get(ch);
      if (ch == '\n') linecount++;
      if (bytecount == 0 && !isgraph(ch)) lastch = ch;
      if (!isspace(lastch) && !iscntrl(lastch) && (isspace(ch)
    || iscntrl(ch) || input_file.eof())) wordcount++;
      if (input_file.eof()) break;
      if (!isspace(ch) && !iscntrl(ch)) charcount++;
      bytecount++;
      lastch = ch;
      }
   if (linecount == 0) linecount = 1;

   cout<< "File" << argv[1] << " contains:\n"
          << dec(linecount,21) << " line(s)\n"
          << dec(wordcount,21) << " words\n" << dec(charcount,21)
          << " characters excluding whitespace\n"
         << dec(bytecount,21) << " bytes\n";
}

/* End of File */