替换文本中的字符串02

*实现替换文件中指定的内容
----Created by cryking----
--------2012.02.12--------*/
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
using namespace std;

char *strstr_rep(char *source, const char *old, const char *ne)//字符替换
{
char *org = source;
char temp[1000];
int old_length = strlen(old);//获得将被替换的字符串的大小
int i, j, k, location = -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)//开始替换
{
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
}
return org;
}
int main()
{
system("rename Ch0_24G.vbs Ch0_24G.txt");
Sleep(3000);

fstream outfile("Ch0_24G.txt", ios::out | ios::in);
char ch;
char buffer[1000];
int i = 0, k = 0;
if (!outfile) {
cout << "不能打开目的文件:test.txt" << '\n';
exit(1);
}

outfile.unsetf(ios::skipws);
while (outfile >> ch) {//将文件全部内容读出到buffer
buffer[i] = ch;
i++;
}
strstr_rep(buffer, "192.168.1.2", "192.168.4.2");//将"2000"替换为8888"
outfile.close();
ofstream infile("Ch0_24G_bak.txt");
while (k != i) { infile << buffer[k]; k++; }//将buffer全部写入到文件
infile.close();

system("del Ch0_24G.txt");
system("rename Ch0_24G_bak.txt Ch0_24G.vbs");

return 0;
}

posted @ 2021-08-25 16:45  江南王小帅  阅读(45)  评论(0)    收藏  举报