69,371
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <fstream>
#include <sstream>
#define WIN32 //VC 编译器开关
#include <time.h>
#ifdef WIN32
# include <windows.h>
#else
# include <sys\time.h>
#endif
#ifdef WIN32
int gettimeofday(struct timeval* tp, void* tzp)
{
time_t clock;
struct tm tm;
SYSTEMTIME wtm;
GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
tm.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm. tm_isdst = -1;
clock = mktime(&tm);
tp->tv_sec = clock;
tp->tv_usec = wtm.wMilliseconds * 1000;
return (0);
}
#endif /* WIN32 */
using std::stringstream;
using std::vector;
using std::string;
using std::fstream;
void help(); // 调用使用帮助
bool isdigit(const std::string& str); // overload function 重载函数
bool isalpha(const std::string& str);
stringstream& load_sstream(stringstream& oss, fstream& infile); // 加载文件到sstream
vector<string> & sortVecData(vector<string> & vecData); // 排序和删除重复
int main(int argc, char* argv[])
{
using namespace std;
if ((1 == argc) || (2 == argc)) { //错误输入处理
help();
return -1;
}
string Value = argv[1];
fstream inFile;
inFile.open(argv[2], ios_base::in);
if (!inFile) {
cerr << "文件错误:不能打开输入文件: " << argv[2] << endl << endl ;
help();
return -1;
}
bool sortData = false; // 排序开关
if (argc >= 4 && ('s' == argv[3][1] || 'S' == argv[3][1])) {
sortData = true;
}
stringstream oss;
ofstream ofDataFile;
ofDataFile.open("LineData.txt"); //保存到 新的数据档文件
long ixold = 0, ixnew = 0;
string::size_type pos;
string strLine;
vector<string> vecLine;
cout << "当前搜索关键字:" << Value << endl;
// 取时间断点
timeval tv;
gettimeofday(&tv, NULL);
double cl = tv.tv_sec + (double)tv.tv_usec / 1000000;
while (getline(inFile , strLine)) { // 整行处理,数据文件 数据输入
if (strcmp(argv[1] , "ALL") == 0 ) { // 所有数据排序用
vecLine.push_back(strLine);
} else {
pos = strLine.find(Value);
if (pos != string::npos) {
vecLine.push_back(strLine); //数据记录到容器
}
}
ixold++; // 旧数据计数器
if (ixold % 100000 == 0) cout << ">";
}
gettimeofday(&tv, NULL);
cl = (tv.tv_sec + (double)tv.tv_usec / 1000000) - cl;
cout << "\n原来数据记录数目:" << ixold << "\t";
cout << "新的数据记录数目:" << vecLine.size() << endl;
cout << "提取数据花费时间:" << cl << " 秒\n";
if (sortData) { // 判断排序
cout << "正在排序优化中,请等候....\n";
gettimeofday(&tv, NULL);
cl = tv.tv_sec + (double)tv.tv_usec / 1000000;
sortVecData(vecLine); //数据排序 删除重复
cout << "排序优化后数据量:" << vecLine.size() << "\t";
gettimeofday(&tv, NULL);
cl = (tv.tv_sec + (double)tv.tv_usec / 1000000) - cl;
cout << "排序数据花费时间:" << cl << " 秒\n";
}
cout << "正在把数据写到新文件中......\t如果数据重复,可以最后加参数 -S 优化排序\n" ;
gettimeofday(&tv, NULL);
cl = tv.tv_sec + (double)tv.tv_usec / 1000000;
vector<string>::iterator iter = vecLine.begin();
while (iter != vecLine.end()) {
oss << *iter++ << endl; // 数据写到缓存流
if (ixnew++ % 100000 == 0) cout << "<"; //新数据计数器
}
ofDataFile << oss.str(); // 缓冲区写文件
gettimeofday(&tv, NULL);
cl = (tv.tv_sec + (double)tv.tv_usec / 1000000) - cl;
cout << "保存新数据文件花费时间:" << cl << " 秒\n";
cout << "\n已经生成新的包含关键字的数据文件 LineData.txt" << endl;
inFile.close();
ofDataFile.close();
return 0;
}
// overload function 重载函数 isdigit
bool isdigit(const std::string& str)
{
bool flag = true;
for (std::string::size_type i = 0; i < str.length(); i++) {
while (!isdigit(str[i])) {
flag = false;
break;
}
}
return flag;
}
bool isalpha(const std::string& str)
{
bool flag = true;
for (std::string::size_type i = 0; i < str.length(); i++) {
while (!isalpha(str[i])) {
flag = false;
break;
}
}
return flag;
}
void help()
{
using std::cerr;
cerr << "本工具可以从文件中按关键字搜索数据行 并且有排序功能 [版本 0.98] BY Hong Wenjun\n\n";
cerr << "示例 1 :D:\\>LineExtraction.exe \"关键字\" D:\\原始数据.txt \n" ;
cerr << "示例 2 :D:\\>LineExtraction.exe \"关键字\" D:\\原始数据.txt -S\n" ;
cerr << "示例 3 :D:\\>LineExtraction.exe ALL D:\\原始数据.txt -S\n\n" ;
cerr << "请输入一个关键字!![ \"关键字\" ALL ] 按两种方式选择一种\n";
cerr << "ALL 关键字 提取所有数据,和-S参数配合排序\n";
cerr << "-S 参数 排序优化,删除重复数据\n";
}
stringstream& load_sstream(stringstream& oss, fstream& infile) // 加载文件到sstream
{
oss << infile.rdbuf();
return oss;
}
vector<string> & sortVecData(vector<string> &vecData) // 排序和删除重复 数字序列
{
sort(vecData.begin(), vecData.end()); //数据排序
vector<string>::iterator end_unique = unique(vecData.begin(), vecData.end()); // 移动重复到最后
vecData.erase(end_unique, vecData.end()); //删除重复
return vecData;
}