【发布时间】:2020-03-10 09:33:23
【问题描述】:
我有一个使用 std::cin 收到的字符串,我试图将 const wchar_t* 更改为该变量
const wchar_t* TARGET_FILE = L"";
try {
std::string str;
std::cout << "DLL Name: ";
getline(std::cin, str);
//TARGET_FILE = str;
std::string narrow_string(str);
std::wstring wide_string = std::wstring(narrow_string.begin(), narrow_string.end());
const wchar_t* result = wide_string.c_str();
const wchar_t* TARGET_FILE = result;
std::cout << TARGET_FILE; //Debug
}
catch (const std::exception & ex) {
std::cout << "[ERROR] " << ex.what() << '\n';
std::cout << "Press any key to exit..." << '\n';
std::cin.get();
return EXIT_FAILURE;
}
当这段代码被执行时,会发生这种情况
DLL Name: Kinky.dll
0000020E11C61560[ERROR] DLL not found.
Press any key to exit...
当我使用硬编码 TARGET_FILE 的值时,我得到 0000019CCB971300 而不是有效的文本格式
//constexpr auto TARGET_FILE = L"Kinky.dll";
对不起,如果我有点愚蠢,但我不太熟悉 wchar_t vars 是什么,我只知道我使用的 sdk 需要一个
编辑: 本部分由我粘贴以尝试解决原始问题,所以如果它的正确用法如果它不正确的用法我希望这是我的问题
std::string narrow_string(str);
std::wstring wide_string = std::wstring(narrow_string.begin(), narrow_string.end());
const wchar_t* result = wide_string.c_str();
const wchar_t* TARGET_FILE = result;
【问题讨论】:
-
这真的是产生运行时错误的代码吗?您有两个不同的
TARGET_FILE变量,一个隐藏另一个,更改第一个的定义不应该有任何影响 -
使用
std::wcout打印宽字符。 -
您将 TARGET_FILE 作为全局变量和局部变量。这可能与您的问题有很大关系。
-
也许运行时错误仅由您未显示的代码引起,该代码假定第一个
TARGET_FILE变量包含文件名,但实际上您从未修改它。确保minimal reproducible example 是必需的 -
尝试在 try-catch 之外添加一个
std::cout << TARGET_FILE; //Debug,这应该可以解决这个问题
标签: c++