练习_文件单词统计.

/*统计文件的行数, 单词数, 字符数, */
#include <stdio.h>

int line(FILE *op);
int word(FILE *op);


int main(int argc, char const *argv[])
{
	FILE *op = NULL;
	int string = 0;
	if((op=fopen("./statistics.c", "r"))==NULL)//打开的是本身这个文件.
	{
		printf("Error!\n");
		return -1;
	}
	while(!feof(op))
	{
		if(fgetc(op)!=EOF)
			string++;
	}
	printf("\n\
					行数=%d\n\
					单词=%d\n\
					字符=%d\n\
					",  line(op), word(op), string);
	fclose(op);
	return 0;
}
/*行数函数*/
int line(FILE *op)
{
	fseek(op, 0, SEEK_SET);
	int line = 0;
	while(!feof(op))
	{
		if(fgetc(op)=='\n')
			line++;
	}
	return line;
}

/*单词数函数*/
int word(FILE *op)
{
	fseek(op, 0, SEEK_SET);
	int word = 0;
	int judge = 0;
	int ch = 0;
	while(!feof(op))
	{
		ch = fgetc(op);
		if(ch==' '&&judge==1)
		{
			judge = 0;
			word++;
		}
		else if(ch!=' ')
			judge = 1;
	}
	return word;
}



转载于:https://my.oschina.net/dengwo/blog/499395

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值