strcmp 不做比较
Strcmp does not do the comparing
为什么字符串之间的比较不起作用?
我确定 user
字符串末尾没有任何结束符,但我仍然知道用户名不被接受。
char user[24];
int userLog = -1;
FILE *usernames;
usernames = fopen("usernames.cfg", "r");
if (usernames == NULL){
perror("usernames - err");
return(-1);
}
while(fgets(user, sizeof(user), usernames) !=NULL){
strtok(user, "\n");
printf("%s -- %s\n", user, possibleUsername);
// First edition of question contained:
// if((possibleUsername, user) == 0)
// Still having problems with this version:
if(strcmp(possibleUsername, user) == 0)
userLog = 1;
else
userLog = 0;
}
if(userLog == 1) printf("Username accepted: %s\n", possibleUsername);
else if(userLog == 0) printf("Username doesn't exist in the database.\n");
fclose(usernames);
usernames.cfg:
user
justplayit
etc
我想应该是
if(strcmp(possibleUsername, user) == 0)
因为表达式
(possibleUsername, user) == 0
等于
user == NULL
编辑
改变
int userLog = -1;
至
int userLog = 0;
并删除
else
userLog = 0;
试试这个:
int main(int argc, char** argv)
{
char user[24];
int userLog;
FILE* usernames;
char* userPtr;
usernames = fopen("usernames.cfg", "r");
if (usernames == NULL)
{
perror("Usernames config not found or read protected");
return EXIT_FAILURE;
}
while(fgets(user, sizeof(user), usernames) != NULL)
{
userPtr = strtok(user, "\n");
if (userPtr != NULL)
{
printf("Username in file => %s", userPtr);
if (strcmp(userPtr, "find me") == 0)
{
userLog = 1;
break;
}
else
{
userLog = 0;
}
}
}
if (userLog)
{
printf("User find me accepted");
}
else
{
printf("User find me not in database");
}
fclose(usernames);
return EXIT_SUCCESS;
}
它与您编写的程序相同,但我使用从 strtok returned 的额外指针来检查是否找到任何标记。将此标记与 "zero-terminated" 字符串进行比较,就像您可能的用户名一样,对我有用。如果可能用户名是固定长度的字符数组,建议您使用 strncmp 并设置要比较的字符串的长度,例如strncmp(userPtr, possibleUsername, length) == 0。如果 usernames.cfg 文件是用 \r\n 保存的,那么 strtok 会 return "username\r" 而不是 "username"。也许你可以调试你的程序并检查用户的缓冲区它有什么内容。希望对你有帮助。
为什么字符串之间的比较不起作用?
我确定 user
字符串末尾没有任何结束符,但我仍然知道用户名不被接受。
char user[24];
int userLog = -1;
FILE *usernames;
usernames = fopen("usernames.cfg", "r");
if (usernames == NULL){
perror("usernames - err");
return(-1);
}
while(fgets(user, sizeof(user), usernames) !=NULL){
strtok(user, "\n");
printf("%s -- %s\n", user, possibleUsername);
// First edition of question contained:
// if((possibleUsername, user) == 0)
// Still having problems with this version:
if(strcmp(possibleUsername, user) == 0)
userLog = 1;
else
userLog = 0;
}
if(userLog == 1) printf("Username accepted: %s\n", possibleUsername);
else if(userLog == 0) printf("Username doesn't exist in the database.\n");
fclose(usernames);
usernames.cfg:
user
justplayit
etc
我想应该是
if(strcmp(possibleUsername, user) == 0)
因为表达式
(possibleUsername, user) == 0
等于
user == NULL
编辑
改变
int userLog = -1;
至
int userLog = 0;
并删除
else
userLog = 0;
试试这个:
int main(int argc, char** argv)
{
char user[24];
int userLog;
FILE* usernames;
char* userPtr;
usernames = fopen("usernames.cfg", "r");
if (usernames == NULL)
{
perror("Usernames config not found or read protected");
return EXIT_FAILURE;
}
while(fgets(user, sizeof(user), usernames) != NULL)
{
userPtr = strtok(user, "\n");
if (userPtr != NULL)
{
printf("Username in file => %s", userPtr);
if (strcmp(userPtr, "find me") == 0)
{
userLog = 1;
break;
}
else
{
userLog = 0;
}
}
}
if (userLog)
{
printf("User find me accepted");
}
else
{
printf("User find me not in database");
}
fclose(usernames);
return EXIT_SUCCESS;
}
它与您编写的程序相同,但我使用从 strtok returned 的额外指针来检查是否找到任何标记。将此标记与 "zero-terminated" 字符串进行比较,就像您可能的用户名一样,对我有用。如果可能用户名是固定长度的字符数组,建议您使用 strncmp 并设置要比较的字符串的长度,例如strncmp(userPtr, possibleUsername, length) == 0。如果 usernames.cfg 文件是用 \r\n 保存的,那么 strtok 会 return "username\r" 而不是 "username"。也许你可以调试你的程序并检查用户的缓冲区它有什么内容。希望对你有帮助。