strstr - string.h错误传达错误?

时间:2014-03-12 00:06:01

标签: c++ c visual-studio winapi string.h

我正在尝试将文件名与字符串列表进行比较以查看它们是否匹配,如果它们相应则返回

我使用以下条件:

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 *)

问题是:上述错误的含义是什么?如何解决?

1 个答案:

答案 0 :(得分:0)

你遇到的问题来自于strstr函数希望看到两个char指针(char *)作为其参数,但它接收到{{1}而数组则作为第一个参数。

与通常的8位字符不同,WCHAR表示16位Unicode字符。

修复错误的一种方法是将Unicode文件名转换为char数组,如下所示:

WCHAR

然后使用char cfile[260]; char DefChar = ' '; WideCharToMultiByte(CP_ACP, 0, file, -1, cfile, 260, &DefChar, NULL); 代替cfile

但这种方法只适用于ASCII字符。

因此,您可以考虑使用另一种适用于file字符串(wstring)的字符串比较方法。

以下代码可能会帮助您实现第二种方法:

WCHAR

关于// 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; } findstd::wsting方法的使用情况的正确回答:{/ 3}}。

有关将string转换为wstring的更多信息:Find method in std::wstring

如果没有帮助,请在评论中留下一些反馈意见。