[英]strstr - string.h miscommunication error?
我正在嘗試將文件名與字符串列表進行比較,以查看它們是否匹配,如果匹配,則相應地返回
我正在使用以下條件:
if (strstr(file, str) != NULL) {
return 1;
}
雖然MSVC ++ 2012在strstr
上提示我以下錯誤:
Error: no instance of overloaded function "strstr" matches the argument list
argument types are: (WCHAR [260], char *)
問題是:上述錯誤的含義是什么,如何解決?
您遇到的問題來自於以下事實: strstr
函數期望將兩個char
指針( char *
)作為其參數,但是它將接收WCHAR
數組作為第一個參數。
與通常的8位字符不同, WCHAR
表示16位Unicode字符。
解決錯誤的一種方法是將Unicode文件名轉換為char數組,如下所示:
char cfile[260];
char DefChar = ' ';
WideCharToMultiByte(CP_ACP, 0, file, -1, cfile, 260, &DefChar, NULL);
然后使用cfile
代替file
。
但是這種方法僅適用於ASCII字符。
因此,您可以考慮使用另一種適合WCHAR
字符串( wstring
)的字符串比較方法。
以下代碼可能會幫助您使用第二種方法:
// Initialize the wstring for file
std::wstring wsfile (file);
// Initialize the string for str
std::string sstr(str);
// Initialize the wstring for str
std::wstring wstr(sstr.begin(), sstr.end());
// Try to find the wstr in the wsfile
int index = wsfile.find(wstr);
// Check if something was found
if(index != wstring::npos) {
return 1;
}
關於在std::wsting
使用find
方法的好答案: std :: wstring中的find方法 。
有關將string
轉換為wstring
更多信息: Mijalko:將std :: string轉換為std :: wstring 。
如果沒有幫助,請在評論中留下一些反饋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.