背景:
公司大数据的某类文件,数据要批量修改
循环搜索文件,并打开文件,修改其指定字符串
#include <iostream>
#include <fstream>
#include<string>
#include<sstream>
#include<io.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cctype>
#include <algorithm>
using namespace std;
int select;
string toUpperString(string str)
{
transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);
return str;
}
string toLowerString(string str)
{
transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower);
return str;
}
char *strstr_rep(char *source,char *old,char *ne)//字符替换
{
char *org=source;
char temp[1000000];
int old_length=strlen(old);//获得将被替换的字符串的大小
int i,j,k,location=-1;
while(1)
{
for(i=0; source[i]&&(location==-1); ++i) //location查找将被替换的字符串的位置
for(j=i,k=0; source[j]==old[k]; j++,k++)
if(!old[k+1])
location=i;
if(location!=-1)//开始替换
{
// cout << "开始替换" <<location << endl;
for(j=0; j<location; j++) //先把被替换的字符串的前一部分COPY到temp
temp[j]=source[j];
for(i=0; ne[i]; i++,j++) //再把替换的新字符串COPY到temp
temp[j]=ne[i];
for(k=location+old_length; source[k]; k++,j++) //把剩下的内容COPY到temp
temp[j]=source[k];
temp[j]=NULL;
for(i=0; source[i]=temp[i]; i++); //把临时字符串temp复制给source
location=-1;
}
else
{
break;
}
}
return org;
}
void work(string str)
{
fstream inputfile(str);
char ch;
char buffer[1000000];
int i=0,k=0;
if(!inputfile)
{
cout<<"不能打开目的文件:" << str <<'\n';
exit(1);
}
inputfile.unsetf(ios::skipws);
while (inputfile>>ch) //将文件全部内容读出到buffer
{
buffer[i]=ch;
i++;
}
if(select==1)
{
cout << "正在处理文件:" << str <<endl;
strstr_rep(buffer,"G\" tle_name","道\" tle_name");
}
else if(select == 2)
{
cout << "正在处理文件:" << str <<endl;
strstr_rep(buffer,"道\" tle_name","G\" tle_name");//将"2000"替换为8888"
}
else
{
cout << "大哥,你选择模式有误!!!" << endl;
return ;
}
inputfile.close();
//system("md ..\\SIDE_LIST_NEW");
ofstream outputfile("00.writing");
//cout << buffer << endl;
while(k!=i)
{
outputfile<<buffer[k]; //将buffer全部写入到文件
//cout << buffer << endl;
k++;
}
inputfile.close();
outputfile.close();
string p ="move /Y 00.writing "+str ;
cout << "*缓存文件正在移动替换:";
system(p.c_str());
}
void getFiles( string path )
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p,q;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do
{
if(!(fileinfo.attrib & _A_SUBDIR))
{
string tt;
tt.assign(fileinfo.name);
string suffixStr = tt.substr(tt.find_last_of('.') + 1);
if(toUpperString(suffixStr)!="XML")
continue;
p.assign(path).append(fileinfo.name);
// unsigned int pos = -1;
work(p);
}
}
while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
int main()
{
//vector<string> files;
//getFiles(".");
cout<<"=============================================================================="<<endl;
cout<<" 数据平台 \"G\" 与 \"道\" 之转换 开发者:62597"<<endl;
cout<<"---------------------------------------------------------------------------"<<endl;
string pathline;
cout << "请输入工作区?(请把程序放在ws_servers\\WS_SVR_RUN目录下)"<<endl;
cout<<"---------------------------------------------------------------------------"<<endl;
cin >> pathline;
cout<<"---------------------------------------------------------------------------"<<endl;
cout << "选择模式:1、把\"G\"改成\"道\" 2、把\"道\"改成\"G\"" << endl;
cin >> select;
cout<<"---------------------------------------------------------------------------"<<endl;
getFiles(".\\"+pathline+"\\data\\station\\SIDE_LIST\\");
cout<<"---------------------------------------------------------------------------"<<endl;
cout << "生成新的文件夹目录:" << ".\\"+pathline+"\\data\\station\\SIDE_LIST\\" <<endl;
cout << "\n任意字符结束..." << endl;
getchar();
getchar();
// char str[30];
// int size = files.size();
// for (int i = 0;i < size;i++)
// {
// cout<<files[i].c_str()<<endl;
// }
return 0;
}