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功能的简单实现的更多相关文章

  1. linux下进度条的简单实现

    在实现进度条之前,先学习一下makefile. 一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中, makefile 定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编 ...

  2. 编程实现类似Linux下cp功能

    MyCP的代码实现 一.题目要求: 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bi ...

  3. Linux下VLAN功能的实现 (转)

    1.Linux网络栈下两层实现 1.1简介     VLAN是网络栈的一个附加功能,且位于下两层.首先来学习Linux中网络栈下两层的实现,再去看如何把VLAN这个功能附加上去.下两层涉及到具体的硬件 ...

  4. Linux下getopt()函数的简单使用

    最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴. 好了,虽然啰嗦了点,但确实是忠告.步入正题: 我们的主角----getopt()函数. 英雄不问出处,getopt()函数的 ...

  5. Linux下好用的简单实用命令

    1.你是否为在输入了一大串命令之后发现第一个字符打错了而苦恼?只能删除重来嘛?或者一步步左移光标? NO,一个组合键轻松搞定 Ctrl+A -----到命令行首 Ctrl+E ------到命令行末 ...

  6. Linux下libaio的一个简单例子

    转载:http://www.cnblogs.com/aLittleBitCool/archive/2011/10/18/2216646.html 异步io,很好玩的一个东西,从接口来看,封装的比较厉害 ...

  7. [转载]Linux下getopt()函数的简单使用

    转载源地址:https://www.cnblogs.com/qingergege/p/5914218.html 1.getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘 ...

  8. 转:Linux下使用Nginx搭建简单图片服务器

    最近经常有人问图片上传怎么做,有哪些方案做比较好,也看到过有关于上传图片的做法,但是都不是最好的,今天再这里简单讲一下Nginx实现上传图片以及图片服务器的大致理念. 如果是个人项目或者企业小项目,仅 ...

  9. shell 脚本实战笔记(11)--Mysql在linux下的安装和简单运维

    前言: linux中安装mysql以及配置的管理, 基础的运维和管理还是需要会一些的. 这边作下笔记, 以求天天向上(^_^). 安装流程:*). 安装mysql-server1). 借助yum检索相 ...

随机推荐

  1. java中创建对象的方法

    有4种显式地创建对象的方式: 1.用new语句创建对象,这是最常用的创建对象的方式. 2.运用反射手段,调用java.lang.Class或者java.lang.reflect.Constructor ...

  2. EBS SOA 修改Web Service参数

    l 需求描述 当把PL/SQL声明Load到ISG,生成WSDL并部署完毕后,需要修改PL/SQL的包头声明部分.例如修改某个过程的参数类型,再重新Load,重新生成WSDL,重新部署.我们会发现PL ...

  3. Android-openFileInput openFileOutput

    Android设计了一套可以操作自身APP目录文件对API openFileInput openFileOutput,读取只需传入文件名,写入需要传入文件名 与 权限模式 界面: 布局代码: < ...

  4. [Oracle]Oracle数据库CPU利用率很高解决方案

    Oracle数据库经常会遇到CPU利用率很高的情况,这种时候大都是数据库中存在着严重性能低下的SQL语句,这种SQL语句大大的消耗了CPU资源,导致整个系统性能低下.当然,引起严重性能低下的SQL语句 ...

  5. C#数据结构汇总

    对C#涉及到的数据结构做了一下简单的汇总,若有遗漏,欢迎补充~~ 还是以学习为目的,在此只是简单的介绍一下,希望对大家能有所帮助,能力有限为了不误导大家,不做详细深入的解析,还望见谅,非常欢迎大大们补 ...

  6. 配置AndroidStdio的开发环境

    http://blog.csdn.net/siwuxie095/article/details/53431818

  7. 比较有用的php代码片段

    一 从网页中提取关键词 $meta = get_meta_tags('http://www.emoticode.net/'); $keywords = $meta['keywords']; // Sp ...

  8. linux进程管理(一)

    进程介绍 程序和进程 程序是为了完成某种任务而设计的软件,比如OpenOffice是程序.什么是进程呢?进程就是运行中的程序. 一个运行着的程序,可能有多个进程. 比如自学it网所用的WWW服务器是a ...

  9. 【12c OCP】最新CUUG OCP-071考试题库(52题)

    52.(12-11) choose the best answer: Examine the structure and data in the PRICE_LIST table: You plan ...

  10. lamp-linux-1

    LAMP编程之Linux(1) LAMP:Linux Apache MySQL PHP LNMP:Linux Nginx MySQL PHP WAMP:Windows Apache MySQL PHP ...