linux下wc功能的简单实现
1.代码来源:自己编写
2.运行环境:linux终端
3.编程语言:c/c++语言
4.bug:未发现
5.当前功能:可以统计字符的字符数、行数、单词数
6.使用方法:wc -l 文件名-->统计行数、wc -w 文件名-->统计但词数、wc -c 文件名-->统计字符数
7.gitbub代码地址:https://github.com/moonzhu/wc
8.实现:
/*
* WC.h
*
* Created on: Sep 9, 2017
* Author: moon
*/
#ifndef WC_H_
#define WC_H_
#include <string>
class WC {
public:
WC();
virtual ~WC();
private:
std::string fileName;
public:
/**
* @function:Setting value of fileName
*/
void setFileName(std::string fileName);
/**
* @function:Counting the number of character
* @return:If success,return the number of character,else return -1
*/
int computingChar(void);
/**
* @function:Counting the number of word
* @return:If success,return the number of word,else return -1
*/
int computingWord(void);
/**
* @function:Counting the number of line
* @return:If success,return the number of line,else return -1
*/
int computingLine(void);
};
#endif /* WC_H_ */
/*
* WC.cpp
*
* Created on: Sep 9, 2017
* Author: moon
*/
#include "WC.h"
#include <fstream>
using namespace std;
WC::WC() {
}
WC::~WC() {
}
void WC::setFileName(string fileName) {
this->fileName = fileName;
}
int WC::computingChar(void) {
std::ifstream in(fileName);
if (!in.is_open())
return 0;
in.seekg(0, std::ios_base::end);
std::streampos sp = in.tellg();
in.close();
return sp;
}
int WC::computingLine(void) {
ifstream in;
int num = 0;
string str;
in.open(fileName);
while (getline(in, str)) {
num++;
}
in.close();
return num;
}
int WC::computingWord(void) {
int num = 0;
char c;
char priorC;
ifstream in;
in.open(fileName);
in.get(c);
priorC = ' ';
while (!in.eof()) {
if (c >= 'a' && c <= 'z') {
if (priorC == ' ' || priorC == '\n' || priorC == '.'
|| priorC == ',' || priorC == ':' || priorC == '?'
|| priorC == '!') {
num++;
}
}
priorC = c;
in.get(c);
}
in.close();
return num;
}
//============================================================================
// Name : wc.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include "WC.h"
#include <string.h>
using namespace std;
int main(int argc, char **argv) {
WC wc;
if (argc < 3)
return -1;
string fileName(argv[2]);
wc.setFileName(fileName);
if (strcmp(argv[1], "-c") == 0) {
cout << wc.computingChar() << endl;
} else if (strcmp(argv[1], "-w") == 0) {
cout << wc.computingWord() << endl;
} else if (strcmp(argv[1], "-l") == 0) {
cout << wc.computingLine() << endl;
} else if (strcmp(argv[1], "-s") == 0) {
}
return 0;
}
linux下wc功能的简单实现的更多相关文章
- linux下进度条的简单实现
在实现进度条之前,先学习一下makefile. 一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中, makefile 定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编 ...
- 编程实现类似Linux下cp功能
MyCP的代码实现 一.题目要求: 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bi ...
- Linux下VLAN功能的实现 (转)
1.Linux网络栈下两层实现 1.1简介 VLAN是网络栈的一个附加功能,且位于下两层.首先来学习Linux中网络栈下两层的实现,再去看如何把VLAN这个功能附加上去.下两层涉及到具体的硬件 ...
- Linux下getopt()函数的简单使用
最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点,但确实是忠告.步入正题: 我们的主角----getopt()函数. 英雄不问出处,getopt()函数的 ...
- Linux下好用的简单实用命令
1.你是否为在输入了一大串命令之后发现第一个字符打错了而苦恼?只能删除重来嘛?或者一步步左移光标? NO,一个组合键轻松搞定 Ctrl+A -----到命令行首 Ctrl+E ------到命令行末 ...
- Linux下libaio的一个简单例子
转载:http://www.cnblogs.com/aLittleBitCool/archive/2011/10/18/2216646.html 异步io,很好玩的一个东西,从接口来看,封装的比较厉害 ...
- [转载]Linux下getopt()函数的简单使用
转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...
- 转:Linux下使用Nginx搭建简单图片服务器
最近经常有人问图片上传怎么做,有哪些方案做比较好,也看到过有关于上传图片的做法,但是都不是最好的,今天再这里简单讲一下Nginx实现上传图片以及图片服务器的大致理念. 如果是个人项目或者企业小项目,仅 ...
- shell 脚本实战笔记(11)--Mysql在linux下的安装和简单运维
前言: linux中安装mysql以及配置的管理, 基础的运维和管理还是需要会一些的. 这边作下笔记, 以求天天向上(^_^). 安装流程:*). 安装mysql-server1). 借助yum检索相 ...
随机推荐
- Python学习-8.Python的循环语句-while语句
例子: i = 1 while i < 10: print(i) i+=1 else: print('finish') 输出1至9和finish 在while语句中同样支持for语句所支持的co ...
- 分布式流式计算平台——S4
本文是作者在充分阅读和理解Yahoo!最新发布的技术论文<S4:Distributed Stream Computing Platform>的基础上,所做出的知识分享. S4是Yahoo! ...
- centos 安装vmware 9.02 报 Failed to load module "pk-gtk-module" "canberra-gtk-module"
http://www.linuxidc.com/Linux/2012-01/50944.htm 系统平台:RHEL6.1 X86 32bit 软件版本:VMware-Workstation-Full- ...
- UWP开发入门(二)——RelativePanel
RelativePanel也是Win10 UWP新增的控件,和上篇提到的SplitView一样在UWP的UI布局起到非常重要的作用.说句实在话,这货其实就是为了UWP的Adaptive UI而特意增加 ...
- 流式处理框架storm浅析(下篇)
本文来自网易云社区 作者:汪建伟 举个栗子 1 实现的目标 设计一个系统,来实现对一个文本里面的单词出现的频率进行统计. 2 设计Topology结构: 这是一个简单的例子,topology也非常简单 ...
- php中的XML DOM(11)
7.创建节点 在dom操作中,增删改操作必须要找父节点 1.DOMElement DOMDocument::createElement ( string $name [, string $value ...
- Vulnhub Billu_b0x
1.信息收集 1.1.获取IP地址: map scan report for 192.168.118.137 Host is up (0.00017s latency). Not shown: 998 ...
- 面向对象之ajax
1.Ajax发送请求的几个步骤 1. 创建 XMLHttpRequest 对象 var xhr = new XMLHttpRequest();//IE6 使用var xhr= new ActiveXO ...
- C语言数据结构之哈夫曼树及哈夫曼编码的实现
代码清单如下: #pragma once #include<stdio.h> #include"stdlib.h" #include <string.h> ...
- saltstack 动态pillar实现
简介 pillar支持的数据存储方式有很多,mysql, mogo,json等.本篇介绍关于http存储方式. 首先简要说明整个流程:salt-master会去一个指定http发送get请求获取一个j ...