2016-01-05 124 views
-2

我有一个字符串路径,用于执行我的文件。它是,例如:更改路径符号至/

E:\folderA\folderB\myfile.exe 

如果我写这个路径,我尝试执行文件那里说,该文件不存在。

当我这样写。然后它工作。

E:/folderA/folderB/myFile.exe 

如何更改\到/?

+2

你需要使用'在C(++),'é逃脱backslashes' :\\ folderA \\ folderB \\ myfile.exe' –

+0

好的,那我怎么把\改成\\在字符串中呢? –

+0

字符串从哪里来? – fredoverflow

回答

0

你可能只是用这个:

std::string str=R"(E:\folderA\folderB\myfile.exe)"; 

,一切都会好起来的

Live Demo

2

对于是否接受Unix(/)或windows(\)分隔符,Windows很古怪。

您还需要逃避 '\' 在一个字符串

const char * bad = "C:\hello\world.txt" 
const char *good = "C:\\hello\\world.txt" 

的std :: string ::更换允许替换。

+1

一个'char *'给一个常量的c字符串。这是不好的形式。你应该使用'std :: string' – NathanOliver

+0

好吧,替换函数应该如何查看,因为'replace(fullPath.begin(),fullPath.end(),'\','\\');'不起作用。 –

+1

@DeKirvis你没有得到逃脱角色的意思,是吗? –

0
GetTempPathA(MAX_PATH, tempPath); 
string fullPath = (string)tempPath + "/" + data.fileName; 

std::replace(fullPath.begin(), fullPath.end(), '\\', '/');