答:
代码如下,这些是读数据和判断的。就是在这里会爆内存的。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <string.h>
#include <time.h>
#include "windows.h"
#include <direct.h>
#include <sys/stat.h>
using namespace std;
//******************************************定义全局变量***********************************************//
string buf_A[2050]; //temp 表缓冲x+2 1024*2+2=2048+2=2050
string buf_B[4098]; //写数据表缓冲x*6 1024*4+2=4096+2=4098 表B大空间可以大大增加命中率
string buf_C[92]; //返回表缓冲
string buf_cmp = ""; //对比temp
int rev_num = 90; //返回表buf_A的数量90左右 少于的话会抛异常 不能有空字符器对比
string a = "访问: DELETE"; //你要查找的东东
string b = "WriteData";
string e = ".tmp"; //这三个是程写入生成的 排除掉
string f = ".TMP";
string g = ".bak";
string j = ".smb";
string y = ".DS_Store";
string k = ":com";
string l = ":AFP";
string temp_str = ""; //temp变量
string temp = "";
string line_temp = "";
char i = 0;
int bufB_num = 0; //单数总和
int prt = 14; //default.ini月份的指针位置是指向14位
long int all_num = 0; //总数
long int vaild_files = 0; //有效文件
char* logA_path = "";
bool flag_do = 0; //有无文件要处理标志位 0代表没有文件 1代表有文件要处理
bool flag_same = 0; //同名标志位
bool flag_add = 0; //同名标志位
bool flag_pressin = 0;
//*****************************************************************************************//
void Exedat() //查检路径打开文件在不在+处理数据
{
char* logpath = "C:\\Users\\work\\Documents\\visual studio 2017\\Projects\\ConsoleApplication1\\Debug\\09.log";
ofstream outfile;
outfile.open(logpath, ios::out | ios::app);
cout << "scaning . . . dat please waiting . . . !" << endl; //成功
fstream file("D:\\Desktop VS\\新建文件夹\\2.txt", ios::in);
if (!file)
{
cout << "Sorry Failed to open file!" << endl;
file.close(); //关闭读文件流
system("pause");
exit(0); //直接结束
}
string line;
int countA = 0;
int add = 0;
//******************************************清空数组********************************************//
countA = 0;
memset(buf_A, 0, sizeof(buf_A)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
bufB_num = 0;
memset(buf_B, 0, sizeof(buf_B)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
memset(buf_C, 0, sizeof(buf_C)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
all_num = 0; //只能在下面清空总数
vaild_files = 0;
temp_str = ""; //temp变量
buf_cmp = "";
temp = "";
line_temp = "";
line = "";
//**********************************************************************************************//
while (getline(file, line))
{
// cout << line << endl; //显示
buf_A[countA] = line;
all_num++;
countA++;
//--------------------------------------------------------------------------------------//
//到这里必须清0 不然会死机抛异常 找了三四天才找到
if (countA >= 2048) //表A上限2050 如果超值清空数组
{
for (int i = 0; i < rev_num; i++) //压到缓存buf_C里 这里必须返回100行给buf_A 因为清空之后 空字符对比会抛异常退出
{
buf_C[i] = buf_A[countA - rev_num + i];
}
countA = 0;
memset(buf_A, 0, sizeof(buf_A)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
for (int i = 0; i < rev_num; i++) //压回到缓存buf_A里
{
buf_A[i] = buf_C[i];
}
countA = rev_num; //返回数量
memset(buf_C, 0, sizeof(buf_C)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
}
//--------------------------------------------------------------------------------------//
//------------------------------------------------------------// 查检是不是写入
if (line.find(a) != string::npos)
{
flag_add = 1;
}
if (flag_add == 1)
{
add++;
if (add == 6)
{
flag_add = 0;
add = 0;
if (line.find(b) != string::npos)
{
flag_pressin = 0; //压入至0
}
else
{
flag_pressin = 1; //压入至1
}
}
}
//------------------------------------------------------------------------------------//
if (flag_pressin == 1) //查找你定义的字符串 如果找不到就返回-1 string::npos
{
// cout << "Found at position "<< endl;
flag_pressin = 0; //压入至0
flag_same = 0;
//*****************************************************//
temp = buf_A[(countA - 10)];
//如果是空白字符串就跳出这一次 不然会抛异常退出 捕获异常后执行
try
{
temp.erase(0, 1); // 清除0前面x个空格字
temp.erase(13, 1); // 从7个字后清除2个空格字
buf_cmp = temp;
}
catch (exception e)
{
// 异常处理区域
cerr << "Capture Exception or Error : " << e.what() << endl; //捕获异常,或然后程序结束
outfile << "Capture Exception or Error ! : " << e.what() << " < " << temp << " >" << endl; //写入log
continue; // 异常处理后,程序会继续执行
}
//*****************************************************//
//查找你定义的字符串 如果找不到就返回-1 string::npos
if ((buf_cmp.find(e) != string::npos) || (buf_cmp.find(f) != string::npos) || (buf_cmp.find(g) != string::npos) || (buf_cmp.find(j) != string::npos) || (buf_cmp.find(y) != string::npos) || (buf_cmp.find(k) != string::npos) || (buf_cmp.find(l) != string::npos))
{
flag_same = 1; //有同名的下面就不干了 排除这几个字
}
else
{
//-----------------------------------------------------------//
for (int i = 0; i < bufB_num; i++) //压到缓存里
{
if (buf_B[i].find(buf_cmp) != string::npos)
{
flag_same = 1; //有同名的下面就跳出
break;
}
}
//-----------------------------------------------------------//
}
//************************************************************************************************//
if (flag_same == 0) //查找你定义的字符串 如果找不到就返回-1 string::npos
{
temp_str = buf_A[(countA - 36)]; // 合并两组字符串
temp_str.erase(0, 2); // 清除前面2个空格字
temp_str = "<" + temp_str + "> "; //加在一起
temp = buf_A[(countA - 16)];
temp.erase(0, 1); // 清除0前面x个空格字
temp.erase(7, 2); // 从7个字后清除2个空格字 汉字是两个字符的
temp_str = temp_str + "<" + temp + "> "; //加在一起
temp = buf_A[(countA - 12)];
temp.erase(0, 1); // 清除0前面x个空格字
temp.erase(9, 2); // 从7个字后清除2个空格字
temp_str = temp_str + "<" + temp + "> "; //加在一起
temp = buf_A[(countA - 10)];
temp.erase(0, 1); // 清除0前面x个空格字
temp.erase(13, 1); // 从7个字后清除2个空格字
temp_str = temp_str + "<" + temp + "> "; //加在一起
temp = buf_A[(countA - 6)];
temp.erase(0, 1); // 清除0前面x个空格字
temp.erase(5, 2); // 从7个字后清除2个空格字
temp_str = temp_str + "<" + temp + ">"; //加在一起
// cout << temp_str << endl;
buf_B[bufB_num] = temp_str; //压到表buf_B
bufB_num++; //自加
vaild_files++; //用效文件
// ofstream outfile;
// outfile.open(logpath, ios::out | ios::app);
outfile << temp_str << endl; //写入
// outfile.close(); //关闭文件流
//**************************************************************//
if (bufB_num >= 4096) //表B上限4098 如果超值清空数组
{
bufB_num = 0;
memset(buf_B, 0, sizeof(buf_B)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
}
}
}
}
outfile.close(); //关闭写文件流
//******************************输出并写日志************************************//
cout << "all line countA: " << all_num << endl;
cout << "filter file countA: " << vaild_files << endl;
//*********************清空数组***********************//
countA = 0;
memset(buf_A, 0, sizeof(buf_A)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
bufB_num = 0;
memset(buf_B, 0, sizeof(buf_B)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
all_num = 0; //只能在下面清空总数
vaild_files = 0;
temp_str = ""; //temp变量
temp = "";
line_temp = "";
line = "";
//****************************************************//
file.close(); //关闭读文件流
}
//*********************************************************//
int main()
{
while(1)
{
Exedat(); //处理数据
}
}