C语言建立用户管理系统

#include 
#include 
#include 

typedef struct
{
    char username[20];
    char userpassworld[20];
}LOADSYSTEM;

void encrypt (char *pwd)
{
    while((*pwd) != '\0')
    {
        *pwd = *pwd^15;
        pwd++;
    }
}

void CreatUser ()
{
    FILE *fp;
    LOADSYSTEM user;
    int i;
    if((fp = fopen("C:\\Users\\lbn\\Desktop\\user.txt", "w")) == NULL)
    {
        printf("File open failed!\n");
        exit(0);
    }
    for(i = 0; i < 3; i++)
    {
         printf("Enter user%d name&password:", i+1);
         scanf("%s%s", user.username, user.userpassworld);
         encrypt(user.userpassworld);
         fprintf(fp, "%s %s\n", user.username, user.userpassworld);
    }
    if(fclose(fp))
    {
        printf("File close failed!\n");
        exit(0);
    }
}

int CompareUser (LOADSYSTEM user)
{
    char name[40], password[20], name1[40];
    int flag;
    FILE *fp;
    if((fp = fopen("C:\\Users\\lbn\\Desktop\\user.txt", "r")) == NULL)
    {
        printf("File open failed!\n");
        return 0;
    }
    strcpy(name, user.username);
    strcpy(password, user.userpassworld);
    encrypt(password);
    strcat(name, " ");
    strcat(name, password);
    strcat(name, "\n");
    while(!feof(fp))
    {
        fgets(name1, 40, fp);
        if((strcmp(name, name1)) == 0)
        {
            flag = 1;
            break;
        }
    }
    if(fclose(fp))
    {
        printf("File close failed!\n");
        exit(0);
    }
    return flag;
}

int main ()
{
    LOADSYSTEM user;
    int result;
    CreatUser();
    printf("Enter username:");
    scanf("%s", user.username);
    printf("Enter userpassword:");
    scanf("%s", user.userpassworld);
    result = CompareUser(user);
    if(result)
    {
        printf("Load success!\n");
    }
    else
    {
        printf("Load faild!\n");
    }
    return 0;
}

你可能感兴趣的:(C++)