好的,这只是一个简单的客户端-服务器程序的一部分。现在我只是想实现一个简单的登录检查器。
在 Users.txt 文件中只有一行:“Bryan”。到目前为止,我无法让 strncmp 产生积极的回应。
#include <stdio.h>
#include <string.h>
#define BUFSIZE 1024
int main()
{
FILE * userf;
userf = fopen("Users.txt", "r");
char usrstr[30];
char buffer[30];
char userbuffer[30];
fgets (buffer, sizeof(buffer), userf); //loads Users.txt in buffer
gets(userbuffer); //type whatever user
printf ("Your login is: %s\n", userbuffer);
if(strncmp (buffer, userbuffer, sizeof(userbuffer)) == 0)
{
sprintf(usrstr, "\nCorrect login received.\n"); // faster than printf
puts(usrstr); //displays content of sprintf above
bzero(usrstr, sizeof(usrstr)); //clears usrstr buffer
printf("buffer is %s.\n",buffer); //for checking
printf("userbuffer is %s.\n",userbuffer); //for checking
}
else
{
sprintf(usrstr, "\nIncorrect login received.\n");
puts(usrstr);
bzero(usrstr, sizeof(usrstr));
printf("buffer is %s.\n",buffer); //for checking
printf("userbuffer is %s.\n",userbuffer); //for checking
}
fclose(userf);
return(0);
}
然后对于我得到的输出:
Bryan
Your login is: Bryan
Incorrect login received.
buffer is Bryan
.
userbuffer is Bryan.
我猜缓冲区后的长间距是关键原因,但我不知道是什么原因造成的。我的编程技能很垃圾,所以任何帮助将不胜感激。