统计文件中的行数、单词数、字符数

用C语言写的代码:

#include<stdio.h>
#include<string.h>
int *getCharNum(char *filename,int *totalNum);
int main()
{
    char filename[30];
    int totalNum[3]={0,0,0};
    printf("Input file name: ");
    scanf("%s",filename);
    if(getCharNum(filename,totalNum))
    {
        printf("Total: %d line,%d words,%d chars\n",totalNum[0],totalNum[2],totalNum[1]);
    }
    else
    {
        printf("Error!\n");
    }
    return 0;
}
int *getCharNum(char *filename,int *totalNum)
{
    FILE *fp;
    char buffer[1003];
    int bufferLen;
    int i;
    char c;
    int isLastBlank=0;
    int charNum=0;
    int wordNum=0;
    if((fp=fopen(filename,"rb"))==NULL)
    {
        perror(filename);
        return NULL;
    }
    printf("line words chars\n");
    while(fgets(buffer,1003,fp)!=NULL)
    {
        bufferLen=strlen(buffer);
        for(i=0;i<bufferLen;i++)
        {
            c=buffer[i];
            if(c==' '||c=='\t')
            {
                !isLastBlank && wordNum++;
                isLastBlank=1;
            }
            else if(c!='\n' && c!='\r')
            {
                charNum++;
                isLastBlank=0;
            }
        }
        !isLastBlank && wordNum++;
        isLastBlank=1;
        totalNum[0]++;
        totalNum[1]+=charNum;
        totalNum[2]+=wordNum;
        printf("%-7d%-7d%d\n",totalNum[0],wordNum,charNum);
        charNum=0;wordNum=0;
    }
    return totalNum;
}

在D盘保存的文件

输出结果为:

posted @ 2016-03-23 17:34  李思雨  阅读(629)  评论(0编辑  收藏  举报