您的位置:首页 > 运维架构 > Linux

模拟linux的命令wc编写的一个函数

2015-06-19 00:50 429 查看
#include <stdio.h>

int main( int argc, char *argv[] )
{
int nl =0;
int nc = 0;
int nw = 0;
int stat = 0;
int next = 0;
FILE *fp = NULL;
char buf[500] = {0};
static int i = 0;
fp = fopen( argv[1], "r" );
char c;
while ( ( c = fgetc( fp )) != EOF  ){
nc++;
if( c == '\n' )
nl++;

if( c != ' ' && c!= '\n' && c != '\t' ){
stat = 1;
buf[i++]=c;
}
if ( (c == ' ' || c == '\n' || c == '\t') && (stat == 1) ){
nw++;
stat = 0;
buf[i] = c;
printf("%s\n",buf);
printf("---------------------\n");
getchar();
}
}
printf("nc=%d nw=%d nl=%d\n",nc,nw,nl);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: