在编程中总会出现 将获取的路径作为参数传递到其他程序中,而获取的路径基本都是\,但是在识别过程中没有对他进行转义,所以会报错。下面就写一下针对路径进行\转义的代码。
#include <iostream>
#include <string>
using namespace std;
//转义函数
int string_replase(string &s1, const string &s2, const string &s3)
{
string::size_type pos = 0;
string::size_type a = s2.size();
string::size_type b = s3.size();
while ((pos = s1.find(s2, pos)) != string::npos)
{
s1.replace(pos, a, s3);
pos += b;
}
return 0;
}
int main() {
//R"()"原样输出。不进行转义
//string str(R"("C:\Users\Administrator\Desktop\test.txt")");//当然也可以直接写成这样,但这样就达不到我们测试的目的了
//std::cout << "str==" << str << std::endl;
string path =R"(\"C:\\Program Files\\AnsysEM\\HFSS15.0\\Win32\\hfss.exe\" -ng -batchsolve C:\\Users\\Administrator\\Desktop\\test\\connector.hfss)";
//在这里就要先进行转义
string_replase(path, "\\\\", "*"); string_replase(path, "\\", ""); string_replase(path, "*", "\\");
std::cout << "path==" << path << std::endl;
return 0;
}