c++操作io常见命令

用c++练习下 系统常见io命令。

1)显示文档的文本
2)统计文本单词数字
3)列出目录所有文件  ,递归思路
4)查找第一个匹配的字符.
5)文本单词排序,  快速排序,其实还是递归思路
6)文本单词排序后去除重复.

 

  除了3和6,可以练下手,其他没太大意义。

 

command.h

#ifndef COMMAND_H_INCLUDED
#define COMMAND_H_INCLUDED

#include <iostream>
#include <fstream>
#include "utility.h"
#include "io.h"
#include "stdio.h"

using namespace std;

class command
{
public:
    int wc(const string&,const char);
    void cat(const string&);
    void ls(const string& _path,int );
    int search_index(const string&,const string&);
    //void ls(const string&);
    vector<string> getwords(const string&);
    vector<string> sort_q(const vector<string>&);

    vector<string> unique_w(const vector<string>&);

};


vector<string> command::unique_w(const vector<string>& _ws)
{
    vector<string> temp;
    string comparestr="";
    for(int i=0;i!=_ws.size();++i)
    {
        if(_ws[i]!=comparestr)
        {
            temp.push_back(_ws[i]);
            comparestr=_ws[i];
        }
    }
    return temp;
}

vector<string> command::getwords(const string& _filename)
{
    vector<string> Ret;
    ifstream in;
    Utility::open_file(in,_filename);
    string line;
    while(getline(in,line))
    {
        int frontP=0;
        for(int i=0;i!=line.size();++i)
        {
            if(line[i]==' '||line[i]==','||i==line.size()-1)
            {
                Ret.push_back(line.substr(frontP,i-frontP));
                frontP=i+1;
            }
        }
    }
    return Ret;
}


vector<string> command::sort_q(const vector<string>& _ws)
{
    return Utility::sort_quick(_ws);
}

 int command::wc(const string& _filename,const char _option)
{
     int RSTcount=0;
    switch(_option)
    {
        case 'w':
            {
                ifstream in;
                Utility::open_file(in,_filename);
                string line;
                while(getline(in,line))
                {
                    for(int i=0;i!=line.size();++i)
                    {
                        if(line[i]==' '||line[i]==',')
                        {
                            ++RSTcount;
                        }
                    }
                    ++RSTcount;
                }
            }
        case 'c':
        {
            //统计字符。
        }
    }
    return RSTcount;
}

void command::cat(const string& _filename)
{
    ifstream in;
    Utility::open_file(in,_filename);
    string line;
    while(getline(in,line))
    {
        cout<<line<<endl;
    }
}


struct dir
{
public:
    int isFolder;
    string name;
};

void command::ls(const string& _path,int l)
{
    string dir=_path+"\\*.*";
    vector<_finddata_t> files;
    _finddata_t tmpfile;

    //测试发现第一个file的句饼一般是.,所以tmpfile不处理.
    long lfDir= _findfirst(dir.c_str(),&tmpfile);

    if(lfDir!=-1l)
    {
        while(_findnext(lfDir,&tmpfile)==0)
        {
            string space;
            for(int i=0;i!=l;++i)
            {
                space+="  ";
            }

            if(tmpfile.attrib==_A_SUBDIR&&(tmpfile.name[0])!='.')//好像有隐藏的.和..文件夹,代表上一个文件夹.这里要去掉.
            {

                printf("%s%s\n",space.c_str(),tmpfile.name);
                ls(_path+"\\"+tmpfile.name,l+1);//习惯了自增++,这里只是要把l+1后传给参数.本身不需要加1.
            }
            else if(tmpfile.attrib!=_A_SUBDIR)//没有文件属性.只能用非文件来替换.以免上面的,上一文件夹..到这里.
            {
                printf("%s%s\n",space.c_str(),tmpfile.name);
            }
        }
    }
    _findclose(lfDir);
}

int command::search_index(const string& _word,const string& _filename)
{
    ifstream in;
    Utility::open_file(in,_filename);
    string line;
    string lines;
    while(getline(in,line))
    {
        lines+=line;
    }

    int position=0;
    bool pitch=false;
    for(position=0;position!=lines.size();++position)
    {
        int index=0;
        pitch=true;
        for(index=0;index!=_word.size();++index)
        {
            if(lines[position+index]!=_word[index])
            {
                pitch=false;
                break;
            }
        }
        if(pitch)
        {
            break;
        }
    }

    if(pitch)
    {
        return position;
    }
    else
    {
        return -1;
    }
}

#endif // COMMAND_H_INCLUDED

  

 

utility.h

#ifndef UTILITY_H_INCLUDED
#define UTILITY_H_INCLUDED
#include <iostream>
#include <fstream>

using namespace std;

namespace Utility{
ifstream& open_file(ifstream&,const string&);
vector<string> sort_quick(const vector<string> _words);

}
ifstream& Utility::open_file(ifstream& _in,const string& _filename)
{
    _in.close();
    _in.clear();
    _in.open(_filename.c_str());
    return _in;
}


//快速排序,性能应该需要优化下.但思路是快速排序思路.
vector<string> Utility::sort_quick(const vector<string> _words)
{
    if(_words.size()>=2)
    {
        string compareStr=_words[0];
        vector<string> left;
        vector<string> right;

        for(int i=1;i!=_words.size();++i)
        {
            if(_words[i]>=compareStr)
            {
                right.push_back(_words[i]);

            }
            else
            {
                left.push_back(_words[i]);
            }
        }

        left=sort_quick(left);
        right=sort_quick(right);

        left.push_back(compareStr);

        for(int i=0;i!=right.size();++i)
        {
            left.push_back(right[i]);
        }
        return left;
    }
    else
    {
        return _words;
    }
}

#endif // UTILITY_H_INCLUDED

 

 

main.cpp

void mainCommand()
{
    command cmd;
    string filename="text.txt";

    //显示文本
    cmd.cat(filename);

    //统计单词数字
    int couta=cmd.wc(filename,'w');
    cout<<"words:"<<couta<<endl;

    //列出目录所有文件
    cmd.ls("E:\\db\\stl",0);

    //查找第一个匹配的字符.
    cout<<cmd.search_index("ittle",filename)<<endl;

    //文本单词排序
    vector<string> Words=cmd.getwords(filename);
    vector<string> sort_words=cmd.sort_q(Words);
    for(int i=0;i!=sort_words.size();++i)
    {
        cout<<sort_words[i]<<endl;
    }

    //文本单词排序后去除重复.
    vector<string> unique_words=cmd.unique_w(sort_words);

    for(int i=0;i!=unique_words.size();++i)
    {
        cout<<unique_words[i]<<endl;
    }

}

 

文件大小

long getFileSize6(const char* strFileName)
{
    std::ifstream in(strFileName);
    if (!in.is_open()) return 0;

    in.seekg(0, std::ios_base::end);
    std::streampos sp = in.tellg();
    return sp;
}

 

 

追加数据。

 

基本步骤:
//读写打开文档.
//定位输出位置.
//存储会被抹去的数据到临时区.
//写入目标数据.
//再补充被抹去的数据.
fstream fs(file.c_str(),ios::in|ios::out);
string line;
fs.seekg(2,ios::beg);
getline(fs,line);
fs.seekp(2,ios::beg);
fs<<"123"<<flush;
fs<<line<<endl;
fs.close();


//ios::beg: 文件开头
//ios::cur: 文件当前位置
//ios::end: 文件结尾

//seek 查询,重定位

//tell 告诉,返回.
//g (get) 输入流,p (put) 输出流.
//3种流,都只有一个状态数据来表示位置.当读写流时要切换位置时候,必须用 seekg或seekp来重定位读写位置.

 

如果读入确定的字符用。is.read:(p,size)

如果不确定字符,用 is.get(p,size,'')

因为测试发现,用 is.get,当只有前2个参数,不知道为什么会少一个字符。可能是默认把最后一个字符当成分隔符,留在流中?

posted @ 2016-09-05 17:40  琴鸟  阅读(555)  评论(0编辑  收藏  举报