在c++的以下函数中,是否可以使用字符串变量来代替TEXT("\\*.txt")?
StringCchCat (szDir, MAX_PATH, TEXT("\\*.txt"));示例:
string ext = "\\*.pdf";
StringCchCat (szDir, MAX_PATH, ext);使用VS时,C++会在编译时返回错误:
error C2664: 'StringCchCatW': can not convert parameter 3 from 'std :: string' to 'STRSAFE_LPCWSTR'发布于 2013-06-30 18:13:52
可以,但必须是一个宽字符串:
std::wstring ext = L"\\*.pdf";
StringCchCat(szDir, MAX_PATH, ext.c_str());除非你在没有定义UNICODE的情况下编译,在这种情况下你这样做是错误的。
发布于 2013-06-30 17:30:52
您的变量ext不是宽字符。实际上,正如@billz所指出的,它是字符串类型。看一看documentation for the StringCchCat function,微软似乎提供了三种重载形式的该函数-一种是标准字符,一种是“TCHAR”,一种是宽字符。无论哪种方式,字符串都不会自动转换为字符类型,这就是为什么应该使用c_str()函数的原因。
https://stackoverflow.com/questions/17388947
复制相似问题