在我的項目,我使用通過指定文件名來加載紋理。現在,我做了這個功能const char* app_dir(std::string fileToAppend);
,返回main
小號argv[0]
並通過fileToAppend
更改應用程序的名稱。由於我無法使用char *輕鬆進行字符串操作,因此我使用std::string
。我的紋理加載器需要一個const char *作爲文件名,所以需要切換回c_str(),現在它會生成一串ASCII符號字符(bug)。我已經通過將app_dir()
的返回類型更改爲std::string
來解決此問題。但爲什麼會發生?的std :: string.c_str()返回一個奇怪的字符
編輯
示例代碼:
//in main I did this
extern std::string app_filepath;
int main(int argc, char** arv) {
app_filepath = argv[0];
//...
}
//on other file
std::string app_filepath;
void remove_exe_name() {
//process the app_filepath to remove the exe name
}
const char* app_dir(std::string fileToAppend) {
string str_app_fp = app_filepath;
return str_app_fp.append(fileToAppend).c_str();
//this is the function the generates the bug
}
我已經通過我前面說過改變其返回類型的std :: string的功能之一。
你能通過發佈有關代碼來澄清你的問題嗎? – 2013-03-16 13:07:06
請提供你的代碼,它會導致它容易根從現在讀碼 – 2013-03-16 13:07:49
@Telkitty看看我的編輯。 – mr5 2013-03-16 13:20:20