C 将密码复制到数组中,然后比较

C 将密码复制到数组中,然后比较,c,C,我试图将密码写入用户键入的文件,保存到一个数组中,这样我就可以比较用户输入的内容,并允许他们登录。 这是我的代码,必须是ANSI标准。目前,我无法获得密码进行比较,并给出正确的输出 #include <stdio.h> #include <stdlib.h> #define NAMELENGTHS 20 #define TEXTLENGTHS 200 #define PASSWORDS 7 struct patients { char forename[NAMELEN

我试图将密码写入用户键入的文件,保存到一个数组中,这样我就可以比较用户输入的内容,并允许他们登录。 这是我的代码,必须是ANSI标准。目前,我无法获得密码进行比较,并给出正确的输出

#include <stdio.h>
#include <stdlib.h>

#define NAMELENGTHS 20
#define TEXTLENGTHS 200
#define PASSWORDS 7

struct patients
{
char forename[NAMELENGTHS];
char lastname[NAMELENGTHS];
int birthDay;
int birthMonth;
int birthYear;
float hight;
float weight;
char conditions[TEXTLENGTHS];
char medication[TEXTLENGTHS];
int vaxno;
char comments[TEXTLENGTHS];

};

void emptyBuffer(void);


int main(void)
{
FILE *fin;
char username[NAMELENGTHS];
char password[PASSWORDS];
char userInput[PASSWORDS];
printf("please enter a username: ");
scanf("%s", username);
emptyBuffer();
if(!(fin = fopen(username, "r")))
{
    printf("user not recognised\n");
    return 1;
}
while(!feof(fin))
{
    fgets(password, PASSWORDS, fin);
}
fclose(fin);
printf("Please enter a password: ");
scanf("%s", userInput);
emptyBuffer();
if(userInput == password)
{
    printf("Successful Login");
}
else
{
    printf("Login Failed please restart");
    return 1;
}
return 0;
}


void emptyBuffer(void)
{
while(getchar() != '\n')
{
    ;
}
}
#包括
#包括
#定义名称长度20
#定义文本长度200
#定义密码7
结构性病人
{
字符名[名称长度];
char lastname[名称长度];
int生日;
出生月份;
int生日;
浮高;
浮重;
字符条件[文本长度];
字符长度[文本长度];
int vaxno;
字符注释[文本长度];
};
无效清空缓冲区(void);
内部主(空)
{
文件*fin;
字符用户名[名称长度];
字符密码[密码];
字符用户输入[密码];
printf(“请输入用户名:”);
scanf(“%s”,用户名);
清空缓冲区();
如果(!(fin=fopen(用户名,“r”))
{
printf(“未识别用户\n”);
返回1;
}
而(!feof(fin))
{
fgets(密码、密码、fin);
}
财务总监(财务);
printf(“请输入密码:”);
scanf(“%s”,用户输入);
清空缓冲区();
if(userInput==密码)
{
printf(“成功登录”);
}
其他的
{
printf(“登录失败,请重新启动”);
返回1;
}
返回0;
}
void emptyBuffer(void)
{
而(getchar()!='\n')
{
;
}
}
您想在用户名后面附加“.txt”。您可以使用strcat
。另外,使用strcmp
比较字符串

printf("Enter user name: ");
// The -4 is to leave room for ".txt"
if (fgets(username, sizeof(username) - 4, stdin)) {
    // fgets leaves newline. remove it
    size_t len = strlen(username);
    if (len > 0 && '\n' == username[len - 1]) username[len - 1] = '\0';
    // Now append .txt
    strcat(username, ".txt");
    // Read the password
    if (!(fin = fopen(username, "r"))) {
        // Remove ".txt" for error message
        username[len - 1] = '\0';
        fprintf(stderr, "user \"%s\" not recognized\n", username);
        return 1;
    }
    // Read the password from the file
    if (fgets(password, sizeof(password), fin)) {
        len = strlen(password);
        if (len > 0 && '\n' == password[len - 1]) password[len - 1] = '\0';
        printf("Enter password: ");
        // Get the password from the user
        if (fgets(userInput, sizeof(userInput), stdin)) {
            len = strlen(userInput);
            if (len > 0 && '\n' == userInput[len - 1]) userInput[len - 1] = '\0';
            // Compare
            if (0 == strcmp(userInput, password)) {
                puts("Successful Login");
            }
            else {
                fclose(fin);
                fputs("Login Failed please restart\n", stderr);
                return 1;
            }
        }
    }
    fclose(fin);
}

未定义的行为。我还没有测试它,但是你传递给
scanf
的参数似乎有点奇怪。为什么在同一个文件上两次调用fopen?
scanf(“%20[^\n]PW.txt”,用户名)
不会将“PW.txt”附加到
用户名
-我认为您正在尝试这样做。@MayhemDaes请编辑问题/帖子并上传您更新/当前的代码