修改要在Printf中使用的文件路径

Roh*_*bra 0 c c++ windows printf

我有一个类似的字符串 "c:\x\y\z"

我想要它的形式 "c://x//y//z"

我尝试使用,stdString.replace("\","//");但它不起作用.

有什么建议?

Luc*_*ore 5

如果你的字符串是"c:\x\y\z",你的字符串中没有\.\表示转义字符.将您的字符串更改为"c:\\x\\y\\z".

另外,请注意如何replace工作 - http://www.cplusplus.com/reference/string/string/replace/

我不认为你可以直接'\\'用两个字符替换一个字符"//".(我可能被证明是错的).

替代方案:

std::stringstream ss;
for ( int i = 0 ; i < str.size() ; i++ )
{
    if ( str[i] == '\\' )
        ss << "//";
    else
        ss << str[i];
}
str = ss.str();
Run Code Online (Sandbox Code Playgroud)