在C和Win32 API中编程:比较字符串 [英] Programming In C and Win32 API: Comparing Strings
问题描述
我在C和Windows API中编写程序。我使用的Visual Studio 2010 Express和字符集设置为未设置。我已经进行了编辑控制来接受用户名。这是声明:
I am writing a program in C and Windows API. I am using Visual Studio 2010 Express and Character Set is set to "Not Set". I have made an edit control to accept username. Here's declaration:
hwnduser = CreateWindow (TEXT("EDIT"), NULL,
WS_VISIBLE | WS_CHILD | WS_BORDER,
220, 70, 80, 20,
hwnd, (HMENU) 3, NULL, NULL);
我将它的值提取到一个名为username的字符串中。
I am fetching its value into a string named username.
len = GetWindowTextLength(hwnduser) + 1;
GetWindowText(hwnduser, username, len);
现在,有效的用户名位于一个名为c_user的字符串中:
Now, the valid username is in a string called c_user:
char c_user[] = "foo";
当我比较它们以检查用户是否使用以下代码输入了有效的用户名,
When I compare them to check if the user has entered valid username using following code,
if (username == c_user)
{
MessageBox(hwnd, "Foo", "Bar", MB_OK);
}
else
{
MessageBox(hwnd, "Bar", "Foo", MB_OK);
}
它从不验证。相反,else条件总是执行!我在哪里犯错误?
It never validates. Instead, the else condition is always executed! Where am I making a mistake?
如何更正?
我试过strcmp!但是,输出不会改变。查看输出(和代码中的比较):
I have tried strcmp! But still, output does not change. See the output(and comparison in code):
推荐答案
C和C ++没有内置的字符串类型,所以你不能以这种方式比较字符串。 C和C ++代替使用字符数组,这种语法只是比较每个数组的地址(不匹配)。
C and C++ have no built-in string type and so you cannot compare strings this way. C and C++ instead use an array of chars and this syntax simply compares the address of each array (which won't match).
而是使用 strcmp()
或 _tcscmp()
。
这篇关于在C和Win32 API中编程:比较字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!