2016-01-05 126 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(), '\\', '/');