我是一个c编程的初学者。虽然我知道如何从"credentials.txt“文件中读取内容,但我不知道如何将用户输入的用户名和密码与文本文件中的内容进行比较。此外,任何建议都将是一个受欢迎的变化。
注册代码(将用户名和密码保存到文件中):
#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;
}登录代码(到目前为止):
#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;
}发布于 2021-08-03 04:40:48
首先,您可以逐行读取文本文件中的数据,并将其存储在两个单独的字符串变量中。即。1)用户名2)密码。
接下来,简单地将username_input与用户名进行比较,密码也是如此。
发布于 2021-08-03 09:48:09
如果您的问题是关于比较来自user和您的credentials.txt的用户名和密码,那么不要将您的用户名和密码存储在两个不同的行中,而是将它们存储在一行中,并通过换行符将它们与其他用户分开,例如
user1,pwd1 \n
user2,pwd2 \n
user3,pwd3 \n然后读取文件并将每一行(例如: user1、pwd1)存储为一个字符串,并使用逗号作为string delimiter并与用户输入进行比较。
https://stackoverflow.com/questions/68630402
复制相似问题