编程在C和Win32 API:比较字符串 [英] Programming In C and Win32 API: Comparing Strings
问题描述
我写在C和Windows API的程序。我使用Visual Studio 2010的前preSS和字符集设置为未设置。我做了一个编辑控件来接受用户名。这里的声明:
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);
我取它的值写入字符串命名的用户名。
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";
当我对它们进行比较,以检查用户是否使用输入有效的用户名以下code,
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);
}
它从来没有验证。相反,其他条件总是执行!我在哪里犯了一个错误?
It never validates. Instead, the else condition is always executed! Where am I making a mistake?
如何纠正呢?
我已经试过的strcmp!但尽管如此,输出不会改变。见(在code和比较)的输出:
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屋!