#include <stdio.h>
#include <stdlib.h>
typedef enum {FALSE = 0,TRUE = 1} bool;
int main(int argc, char* argv[])
{
char buf[ 100 ];
getcwd(buf, 100);
printf("current path is: %s\n", buf);
FILE* fp = NULL;
int byte_count =0, word_count =0, line_count =0;
int ch;
bool last_char_is_word = FALSE;
/* if there is a command line arg, then try to open it as the file
otherwise, use stdin */
fp = stdin;
if (argc == 2)
{
fp = fopen(argv[1],"r");
if (fp == NULL)
{
fprintf(stderr,"Unable to open file %s. Exiting.\n",argv[1]);
exit(1);
}
}
/* read until the end of file, counting chars, words, lines */
while ((ch = fgetc(fp)) != EOF)
{
if (ch == '\n')
{
line_count++;
if(last_char_is_word)
word_count++;
last_char_is_word=FALSE;
}
else if (ch == '\t' || ch == ' ')
{
if(last_char_is_word)
word_count++;
last_char_is_word=FALSE;
}
else
last_char_is_word=TRUE;
byte_count++;
}
/* all done. If the input file was not stdin, close it*/
if (fp != stdin)
{
fclose(fp);
}
printf("%d\t%d\t%d\t%s",line_count,word_count,byte_count,argv[1]);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
my_wc.rar_c实现wc命令_linux wc_linux wc -l
共12个文件
my_wc:2个
o:2个
readme:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 0 下载量 43 浏览量
2022-09-22
23:58:26
上传
评论
收藏 13KB RAR 举报
温馨提示
用C语言实现linux的wc命令,统计文件的行数,字符数,词组数,格式为my_wc filename
资源详情
资源评论
资源推荐
收起资源包目录
my_wc.rar (12个子文件)
my_wc
bin
Release
my_wc 5KB
Debug
my_wc 10KB
test~ 523B
main.c 1KB
my_wc.layout 236B
obj
Release
main.o 2KB
Debug
main.o 5KB
test 1KB
readme~ 394B
my_wc.cbp 1KB
my_wc.depend 110B
readme 523B
共 12 条
- 1
钱亚锋
- 粉丝: 86
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论10