home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gold Fish 1
/
GoldFishApril1994_CD1.img
/
d1xx
/
d162
/
cli_utilities
/
wordcount
/
wordcount.c
< prev
Wrap
C/C++ Source or Header
|
1988-10-02
|
2KB
|
82 lines
/* Wordcount (C) Copyright 1987 by Russell Wallace. This program is in
the public domain and may be freely distributed. It may not be used
commercially without the author's permission. If you find this program
useful,please send a small donation to the author at:
24 Lower Georges St.
Dunlaoghaire
Co. Dublin
Ireland
to help finance further software development. Contributions in kind i.e.
Amiga disks welcome. If you wish to discuss any aspect of the Amiga or
computers in general,write or phone 807094.
Usage: Wordcount <filename>
The number of words in the text file referred to, the length of the file
and the average word length (rounded down to the nearest integer) will be
printed to the CLI window.
If you give copies of this program to anyone,please distribute source
code with object code to allow examination of programming techniques;
the Amiga programming environment is so complex that source code is
very valuable as an information source */
#include <libraries/dos.h>
#include <exec/types.h>
#define MAXBUFFER 256L /* length of I/O buffers */
#define EOF '\0'
char inbuffer[MAXBUFFER]; /* input buffer */
struct FileInfoBlock *fp; /* pointer to file */
char charin () /* input a character */
{
static short inpoint=MAXBUFFER-1;
static short maxin =MAXBUFFER-1;
LONG Read ();
char ch;
if (++inpoint>=maxin) /* If buffer used up, */
{ /* refill it from file */
maxin=Read (fp,inbuffer,MAXBUFFER);
inpoint=0;
}
if (maxin==0) /* No more characters to read, so return EOF */
return (EOF);
return (inbuffer[inpoint]); /* else return next character */
}
main (argc,argv)
int argc; /* number of arguments input by user */
char *argv[]; /* pointers to each argument */
{
struct FileInfoBlock *Open ();
char ch,oldch,charin ();
unsigned long length,wordcount;
if ((argc<2)||(*argv[1]=='?')) /* If no arguments or ?,give help */
{
printf ("Usage: Wordcount <filename>\n");
exit (10);
}
if ((fp=Open (argv[1],MODE_OLDFILE))==0) /* Can't open file */
{
printf ("Can't open file\n");
exit (10);
}
length=0;
wordcount=1;
ch='\0';
for (;;)
{
oldch=ch;
if (!(ch=charin ()))
break;
length++;
if (ch==' ' && oldch!=' ')
wordcount++;
}
printf ("Number of words = %lu\n",wordcount);
printf ("Length of file = %lu\n",length);
printf ("Average word length = %lu\n",(long)((length-wordcount)/wordcount));
Close (fp);
}