gpt4 book ai didi

c - 根据输出搜索 .txt 文件

转载 作者:行者123 更新时间:2023-11-30 17:41:51 24 4
gpt4 key购买 nike

我制作了一个密码生成器...然后生成一个 12 个字符的密码,然后将密码保存到 .txt 文件中。我希望能够搜索 .txt 文件,这样就不会创建相同的两个密码。老实说,我不知道如何解决这个问题,并且非常感谢一些帮助。

我的密码生成器正在工作,并且密码保存得很好。

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


FILE*fp;

char *symbols = "#~!$%^&*()+=<>?/@"; // Random Symbol is generated form this list

int main(void) {

char password[4 + 4 + 2 + 2 + 1]; // 4 Upper, 4 Lower, 2 Num, 2 Symbols
int i, j=0, len=sizeof(password)-1;
int menuNum = 0;
char passwordCheck[15+1];

fp = fopen("passwords.txt", "a+"); //Opens the text file to save the Passwords

srand(time(NULL));
printf(" Main Menu\n");
printf("********************************\n");
printf("\nEnter 1 to Generate a New Password: ");
printf("\n\n");
printf("Enter 2 to Check Old Passwords: ");
printf("\n\n");
printf("Enter 3 to Exit. ");
printf("\n\n");
scanf("%d", &menuNum); // reads number

if (menuNum == 1) // If 1 is entered on the Menu...
{
printf("********************************\n");
printf("\nYour New Password is: \n\n");

// Each Password will Have 12 Characters(4 Uppercase letters, 4 Lowercase letters, 2 Numbers & 2 Symbols)

for (i = 0; i < 4; ++i)
password[j++] = 'a' + rand() % ('z' - 'a' + 1); // Generates 4 random Lowercase characters

for (i = 0; i < 4; ++i)
password[j++] = 'A' + rand() % ('Z' - 'A' + 1); // Generates 4 random Uppercase characters

for (i = 0; i < 2; ++i)
password[j++] = '0' + rand() % ('0' - '9' + 1); // Generates 2 random numbers

for (i = 0; i < 2; ++i)
password[j++] = symbols[rand() % strlen(symbols)]; // Generates 2 random symbols

password[j] = '\0';
for(i = 0; i < sizeof(password)-1; ++i)
{
char c = password[i];
j = rand() % len;
password[i] = password[j];
password[j] = c;
} // Shuffles passwords Characters

printf("%s\n\n", password);
printf("********************************\n");
fprintf(fp, "\n%s", password); // Outputs the Generated Passoword to the text file
fclose(fp); // Closes the text file
system("pause");
}

else if (menuNum == 2)
{
printf("\nEnter your password for checking: ");
printf("%s", passwordCheck); // reads password
if (strlen(passwordCheck) > 15) // Checks length of Password
{
printf("'%s' is too long. Needs to be less then 15 Characters\n", passwordCheck);
system("pause");
}
else if (strlen(passwordCheck) < 9) //Checks length of Password
{
printf("'%s' is too short. Needs to be more then 9 Characters\n", passwordCheck);
system("pause");
}
}

}

最佳答案

您应该一一读取文件中的所有字节并将字符保存在某种缓冲区中,直到遇到换行符。然后将缓冲区中的所有字节与您要添加的密码进行比较,如果相同,则该密码已经在文件中。清除缓冲区并读取直到下一个换行符。重复直到文件末尾。

关于c - 根据输出搜索 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20974137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com