文件的字数统计程序
实例说明
编程实现统计一个或多个文件的行数、字数和字符数。程序运行结果如图 所示(设该程序文件名为 program.exe)。
实例解析
一个行由一个换行符限定,一个字由空格分隔(包括空白符、制表符和换行符),字符是指文件中的所有字符。要求程序另设 3 个任选的参数,让用户指定所要统计的内容。
1——统计文件行数;w——统计文件字数;c——统计文件字符数。若用户未指定任选的参数,则表示 3 个统计都要。
运行本程序时的参数按以下格式给出:
-l -w -c 文件 1 文件 2 … 文件 n
其中,前 3 个任选参数 l、w、c 的出现与否和出现顺序任意,或任意组合在一起出现,如-lwc,-cwl,-lw,-wl,-lc,-cl,-cw 等。
为实现上述功能,程序引入 3 个计数器,分别用于统计行数、字数和字符数。行计数器在遇到字符行的第一个字符时增 1;字计数器在遇到每个字时增 1;字符计数器在遇到每个字符时增 1。
为标识一行的开始和结束,引入一个标志变量,遇换行符时,该标志变量置 0,遇其他字符时,行计数器增 1,并置标志变量为 1;为表示一个字的开始和结束,也引入一个标志变量,遇空白类字符时,该标志变量置 0,遇其他字符时,字计数器增 1,并置标志变量为 1。程序另引入 3 个标志变量分别用于区分程序要统计的内容。
程序代码
#include <stdio.h>
main(int argc, char **argv )
{
FILE *fp;
int lflg,wflg,cflg; /* l, w, c 3 个标志 */
int inline,inword; /* 行内和字内标志 */
int ccount,wcount,lcount; /* 字符,字,行 计数器 */
int c;
char *s;
lflg=wflg=cflg=0;
if(argc<2)
{
printf("To run this program, usage: program -l -w -c file1 file2 ... filen \n");
exit(0);
}
while(--argc>=1&&(*++argv)[0]=='-')
{
for(s=argv[0]+1;*s!='\0';s++)
{
switch(*s)
{
case 'l':
lflg=1;
break;
case 'w':
wflg=1;
break;
case 'c':
cflg=1;
break;
default:
puts("To run this program, usage: program -l -w -c file1 file2 ...filen");
exit(0);
}
}
}
if(lflg==0&&wflg==0&&cflg==0)
lflg=wflg=cflg=1;
lcount=wcount=ccount=0;
while(--argc>=0)
{
if((fp=fopen(*argv++,"r"))==NULL) /* 以只读方式打开文件 */
{
fprintf(stderr,"Can't open %s.\n",*argv);
continue;
}
inword=inline=0;
while((c=fgetc(fp))!=EOF)
{
if(cflg)
ccount++;
if(wflg)
if(c=='\n'||c==' '||c=='\t')
inword=0;
else if(inword==0)
{
wcount++;
inword=1;
}
if(lflg)
if(c=='\n')
inline=0;
else if(inline==0)
{
lcount++;
inline=1;
}
}
fclose(fp); /* 关闭文件 */
}
if(lflg)
printf(" Lines = %d\n",lcount);
if(wflg)
printf(" Words = %d\n",wcount);
if(cflg)
printf(" Characters = %d\n",ccount);
}
归纳注释
程序为了能同时对多个文件完成统计,把文件名参数定在最后,根据 main 函数的数组指针参数依次对多个文件进行统计。