You could use lex to do this. The O'Reilly book on lex & yacc (Levine,
Mason and Brown) contains the following
lex sample to count words and lines in a file. (I haven't run it but I
assume it works).

%{
unsigned charCount = 0, wordCount = 0, lineCount = 0;
%}

word [^ \t\n]+
eol  \n
%%
{word}    { wordCount++; charCount += yyleng; }
{eol}     { charCount++; lineCount++; }
.    charCount++;
%%
main(argc,argv)
int argc;
char **argv;
{

     if (argc > 1) {
          FILE *file;

          file = fopen(argv[1], "r");
          if (!file) {
               fprintf(stderr,"could not open %s\n",argv[1]);
               exit(1);
          }
          yyin = file;
     }
     yylex();
     printf("%d %d %d\n",charCount, wordCount, lineCount);
     return 0;
}









Novak Elliott <[EMAIL PROTECTED]> on 07/28/98 04:44:12 AM

Please respond to Novak Elliott <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:    (bcc: Anthony Simon/THP)
Subject:  unix wc




hi,
     does anyone know where to find the source code for wc ? All i
really want is a nice utility function that takes a string or file pointer
as an argument and returns the number of lines in the file (although it
might be useful to also pass it a flag indicating to return number of
words, lines, etc).

thanx in advance,
novak.







Reply via email to