首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++中的注册和登录系统

C++中的注册和登录系统
EN

Stack Overflow用户
提问于 2021-08-03 04:32:08
回答 2查看 53关注 0票数 0

我是一个c编程的初学者。虽然我知道如何从"credentials.txt“文件中读取内容,但我不知道如何将用户输入的用户名和密码与文本文件中的内容进行比较。此外,任何建议都将是一个受欢迎的变化。

注册代码(将用户名和密码保存到文件中):

代码语言:javascript
运行
复制
#include <stdio.h>
#include <string.h>

int main()
{
    char username[19], password[19];

cred_enter_username:
    printf("Enter username (Max 18 characters): ");
    int len_u = strlen(gets(username));

    for (int i = 0; username[i] != '\0'; i++)
    {
        if (username[i] == ' ')
        {
            printf("No spaces allowed. Use only '_' or '-'. Try again\n");
            goto cred_enter_username;
        }
    }

    if (len_u > sizeof(username))
    {
        printf("\n");
        printf("Number of characters exceed allowed characters. Please try again");
        printf("\n\n");
        goto cred_enter_username;
    }

cred_enter_password:
    printf("Enter password (Max 18 characters): ");
    int len_p = strlen(gets(password));

    for (int i = 0; password[i] != '\0'; i++)
    {
        if (password[i] == ' ')
        {
            printf("No spaces allowed. Try again\n");
            goto cred_enter_password;
        }
    }

    if (len_p > sizeof(password))
    {
        printf("\n");
        printf("Number of characters exceed allowed characters. Please try again");
        printf("\n\n");
        goto cred_enter_password;
    }

    FILE *fptr = NULL;
    fptr = fopen("credentials.txt", "a");

    fprintf(fptr, "Username: %s\n", username);
    fprintf(fptr, "Password: %s\n", password);
    fprintf(fptr, "\n");

    fclose(fptr);

    return 0;
}

登录代码(到目前为止):

代码语言:javascript
运行
复制
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    FILE *credentials;
    char *buffer;
    long numbytes;
    char username[19], password[19];
    char userstr[30], passstr[30];

    credentials = fopen("credentials.txt", "r");
    if (credentials == NULL)
    {
        printf("No account found. Sign up first");
        return 1;
    }

    fseek(credentials, 0L, SEEK_END);
    numbytes = ftell(credentials);

    fseek(credentials, 0L, SEEK_SET);

    buffer = (char *)calloc(numbytes, sizeof(char));

    if (buffer == NULL)
    {
        printf("Memory Error");
        return 1;
    }

    fread(buffer, sizeof(char), numbytes, credentials);

cred_enter_username:
    printf("Enter username (Max 18 characters): ");
    int len_u = strlen(gets(username));

    for (int i = 0; username[i] != '\0'; i++)
    {
        if (username[i] == ' ')
        {
            printf("Invalid username. Try again\n");
            goto cred_enter_username;
        }
    }

    if (len_u > sizeof(username))
    {
        printf("\n");
        printf("Number of characters exceed allowed characters. Please try again");
        printf("\n\n");
        goto cred_enter_username;
    }
    
cred_enter_password:
    printf("Enter password (Max 18 characters): ");
    int len_p = strlen(gets(password));

    for (int i = 0; password[i] != '\0'; i++)
    {
        if (password[i] == ' ')
        {
            printf("Invalid. Try again\n");
            goto cred_enter_password;
        }
    }

    if (len_p > sizeof(password))
    {
        printf("\n");
        printf("Invalid password. Try again");
        printf("\n\n");
        goto cred_enter_password;
    }

    return 0;
}
EN

回答 2

Stack Overflow用户

发布于 2021-08-03 04:40:48

首先,您可以逐行读取文本文件中的数据,并将其存储在两个单独的字符串变量中。即。1)用户名2)密码。

接下来,简单地将username_input与用户名进行比较,密码也是如此。

票数 0
EN

Stack Overflow用户

发布于 2021-08-03 09:48:09

如果您的问题是关于比较来自user和您的credentials.txt的用户名和密码,那么不要将您的用户名和密码存储在两个不同的行中,而是将它们存储在一行中,并通过换行符将它们与其他用户分开,例如

代码语言:javascript
运行
复制
user1,pwd1 \n
user2,pwd2 \n
user3,pwd3 \n

然后读取文件并将每一行(例如: user1、pwd1)存储为一个字符串,并使用逗号作为string delimiter并与用户输入进行比较。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68630402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档