简体   繁体   English

C++ wchar_t* 不工作

[英]C++ wchar_t* not working

    wchar_t* Pfad;
    wcin >> Pfad;

wchar_t* file = Pfad + "*.quiz";

Not working for me, how can I make this work?不适合我,我怎样才能使它工作? It says "*.quiz" is wrong, something like it has to be a numeric value or something like that.它说"*.quiz"是错误的,比如它必须是一个数值或类似的东西。

Well sorry, I am new to c++...好吧,对不起,我是 C++ 的新手...

and my actual problem is, I use this, but I want to user to select the path我的实际问题是,我使用这个,但我想让用户选择路径

something like cin >> file类似cin >>文件的东西

but I don't want the user to have to type "*.quiz";但我不希望用户必须输入“*.quiz”;

wchar_t* file = L"C:/Users/Niku/Documents/Visual Studio 2012/Projects/Quiz/Quiz/Fragen_Niko/*.quiz"; 
WIN32_FIND_DATA FindFileData;
 HANDLE hFind;
hFind = FindFirstFile(file, &FindFileData); 

if( hFind != INVALID_HANDLE_VALUE ) {
max++;
while ((x = FindNextFile(hFind, &FindFileData)) == TRUE)
max++;
}

If I use anything else besides wchar_t* it's not working..如果我使用 wchar_t* 之外的任何其他东西,它就不起作用..

It's not working the way you are expecting because you don't fully understand pointers and string literals in C++.它不像您期望的那样工作,因为您不完全理解 C++ 中的指针和字符串文字。 If you want to know what's wrong, please pick any of these books (possibly beginner ones).如果您想知道出了什么问题,请选择这些书籍中的任何一本(可能是初学者的)。

If you want to fix this problem straight ahead (take a shortcut) use an std::wstring instead:如果您想直接解决此问题(走捷径),请改用std::wstring

std::wstring Pfad;
std::wcin >> Pfad;
auto file = Pfad + L"*.quiz";

Live demo现场演示

As suggested by Rook , if you have to pass a pointer to C APIs you can use the member function std::wstring::c_str , which will return a const wchar_t* for you to use.正如Rook所建议的,如果您必须将指针传递给 C API,您可以使用成员函数std::wstring::c_str ,它将返回一个const wchar_t*供您使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM