读书人

共享代码:按关键字提取一行记录 le h

发布时间: 2012-03-01 10:25:47 作者: rapoo

共享代码:按关键字提取一行记录 le hongwenjun www.csdn.net.sql -S


C/C++ code
#include <iostream>#include <fstream>#include <string>#include <vector>#include <algorithm>int main(int argc, char* argv[]){    using namespace std;    //错误输入处理    if ((1 == argc)||(2 == argc)) {        cerr << "示例 1 :D:\\>LineExtraction.exe  \"关键字\"   D:\\原始数据.txt  \n" ;        cerr << "示例 2 :D:\\>LineExtraction.exe  \"关键字\"   D:\\原始数据.txt  -S\n" ;        cerr << "示例 3 :D:\\>LineExtraction.exe  ***  D:\\原始数据.txt  -S\n\n" ;        cerr << "请输入一个关键字!! \n";        cerr << "-S 参数排序优化;  *** 关键字 提取所有的数据 ";        return -1;    }    string Value = argv[1];    ifstream inFile;    inFile.open(argv[2]);    if (!inFile) {        cerr << "文件错误:不能打开输入文件: " << argv[2] << endl <<endl ;        cerr << "示例 1 :D:\\>LineExtraction.exe  \"关键字\"   D:\\原始数据.txt  \n" ;        cerr << "示例 2 :D:\\>LineExtraction.exe  \"关键字\"   D:\\原始数据.txt  -S\n" ;        cerr << "示例 3 :D:\\>LineExtraction.exe  ***  D:\\原始数据.txt  -S\n\n" ;        cerr << "请输入一个关键字!! \n";        cerr << "-S 参数排序优化;  *** 关键字 提取所有的数据 ";        return -1;    }    bool sortLine = false;     // 排序开关    if (argc >= 4 && ('s' == argv[3][1] || 'S' == argv[3][1])) {        sortLine = true;    }    ofstream ofLineFile;    ofLineFile.open("LineData.txt"); //保存到 新的数据档文件    long ixold = 0, ixnew = 0;    string::size_type pos;    string  strLine;    vector<string> vecLine;    cout << "当前提取关键字:" << Value << endl;   while(getline(inFile , strLine)){     //整行处理,以备修改        pos = strLine.find(Value);        if (pos != string::npos || '*' == argv[1][0]) {   //数据是否有关键字 Value            vecLine.push_back(strLine);   //数据记录到容器        }        ixold++;  //旧数据计数器        if (ixold % 100000 == 0) cout << ">";    }    cout << "\n原来数据记录数目:" << ixold         << "\n新的数据记录数目:" << vecLine.size() << endl;    if (sortLine) {        cout << "正在排序优化中,请等候....\n";        sort(vecLine.begin(), vecLine.end());  //数据排序        vector<string>::iterator end_unique = unique(vecLine.begin(), vecLine.end());  // 移动重复到最后        vecLine.erase(end_unique, vecLine.end());  //删除重复        cout << "排序和剔除重复后的实际数量:" << vecLine.size() << endl;    }    cout << "正在把数据写到新文件中......     如果数据重复,可以最后加参数 -S 优化排序\n" ;    vector<string>::iterator iter = vecLine.begin();    while (iter != vecLine.end()) {        ofLineFile << *iter++ << endl;  //数据写到文件        if (ixnew++ % 100000 == 0) cout << "<";    //新数据计数器    }    cout << "\n已经生成新的包含关键字的数据文件 LineData.txt" << endl;    inFile.close();    ofLineFile.close();    return 0;}


编译成 le.exe 然后 执行搜索命令
C/C++ code
C:\temp>le hongwenjun www.csdn.net.sql -S当前提取关键字:hongwenjun>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>原来数据记录数目:6428632新的数据记录数目:6正在排序优化中,请等候....排序和剔除重复后的实际数量:6正在把数据写到新文件中......     如果数据重复,可以最后加参数 -S 优化排序<已经生成新的包含关键字的数据文件 LineData.txt


我 2007年注册,搜索出的六条记录 都不是我的记录


[解决办法]
牛啊,我差点也想写一个,后来想想还是没动手。。。


[解决办法]
CSDN泄露出的用户信息给大家提供了无比宝贵的练习资料,哈哈!

读书人网 >C++

热点推荐