C密码数据库
C password database
我创建了一个程序,如果您输入以前的密码,该程序可以让您从文件中更改密码 file.What我想做的是能够创建一个已分配的用户名使用 password.The 用户名和密码应该写入文件而不删除那里的任何内容 before.The 程序还应该能够验证 file.Here 中用户名的密码是我当前的代码,但我无法在给定的 file.I 中写出多个东西,不想让你给我我的问题的代码,只有一些 tips.Thank 你的算法!
#include<stdio.h>
#include <conio.h>
#include <fstream>
#include <string.h>
#include <stdlib.h>
int main()
{
FILE *passwords;
int p='*',i,j,count,triesLeft,a,numberofTries=0;
char password[5] = {0,0,0,0,0};
char passwordCheck[5] = {0,0,0,0,0};
passwords = fopen("passwords.txt","r");
printf("You have 3 tries to enter your password!\n");
for(count=0;count<3;count++)
{
numberofTries++;
triesLeft = 3 - count;
printf("You have %d tries left!\n", triesLeft);
printf("Enter your password: ");
scanf("%s", &passwordCheck);
fscanf(passwords,"%s",&password);
if(strcmp(password, passwordCheck) == 0)
{
numberofTries--;
printf("Press 0 if you want to set up a new password, press 1 to stop the program\n");
scanf("%d", &a);
if(a==0)
{
passwords = fopen("passwords.txt","w");
printf("New password:");
for(i=0;i<5;i++)
{
password[i] = getch();
putchar(p);
}
for(j=0;j<5;j++)
{
fprintf(passwords,"%c",password[j]);
}
}
else if(a==1)
{
printf("Old password still in place");
}
break;
}
else
{
printf("Wrong password!");
}
}
if(numberofTries == 3)
{
printf("You are out tries!");
}
fclose(passwords);
}
看看 fopen documentation. The magic phrase here is "access mode". As you open the file, the FILE pointer points to a certain position inside of the file. By setting the appropriate access mode, you can choose where the position pointer will be placed when the file is opened. Maybe the function fseek 对你来说也很有趣。它允许您移动该指针。
关于您的代码的更多提示:
- 确保你没有使用很多不必要的变量,因为
它们会使您的代码变得混乱。
- 使用fopen时,一定要检查指针设置是否正确
(检查 NULL),否则你的程序可能会崩溃
如果无法找到或访问文件,则会出现分段错误。
- 在 C 中,不同于 0 或 NULL 的所有内容都为真。那适用于
关于数值的指针。随意使用否定
操作员 ”!”结合这样的测试。
我创建了一个程序,如果您输入以前的密码,该程序可以让您从文件中更改密码 file.What我想做的是能够创建一个已分配的用户名使用 password.The 用户名和密码应该写入文件而不删除那里的任何内容 before.The 程序还应该能够验证 file.Here 中用户名的密码是我当前的代码,但我无法在给定的 file.I 中写出多个东西,不想让你给我我的问题的代码,只有一些 tips.Thank 你的算法!
#include<stdio.h>
#include <conio.h>
#include <fstream>
#include <string.h>
#include <stdlib.h>
int main()
{
FILE *passwords;
int p='*',i,j,count,triesLeft,a,numberofTries=0;
char password[5] = {0,0,0,0,0};
char passwordCheck[5] = {0,0,0,0,0};
passwords = fopen("passwords.txt","r");
printf("You have 3 tries to enter your password!\n");
for(count=0;count<3;count++)
{
numberofTries++;
triesLeft = 3 - count;
printf("You have %d tries left!\n", triesLeft);
printf("Enter your password: ");
scanf("%s", &passwordCheck);
fscanf(passwords,"%s",&password);
if(strcmp(password, passwordCheck) == 0)
{
numberofTries--;
printf("Press 0 if you want to set up a new password, press 1 to stop the program\n");
scanf("%d", &a);
if(a==0)
{
passwords = fopen("passwords.txt","w");
printf("New password:");
for(i=0;i<5;i++)
{
password[i] = getch();
putchar(p);
}
for(j=0;j<5;j++)
{
fprintf(passwords,"%c",password[j]);
}
}
else if(a==1)
{
printf("Old password still in place");
}
break;
}
else
{
printf("Wrong password!");
}
}
if(numberofTries == 3)
{
printf("You are out tries!");
}
fclose(passwords);
}
看看 fopen documentation. The magic phrase here is "access mode". As you open the file, the FILE pointer points to a certain position inside of the file. By setting the appropriate access mode, you can choose where the position pointer will be placed when the file is opened. Maybe the function fseek 对你来说也很有趣。它允许您移动该指针。
关于您的代码的更多提示:
- 确保你没有使用很多不必要的变量,因为 它们会使您的代码变得混乱。
- 使用fopen时,一定要检查指针设置是否正确 (检查 NULL),否则你的程序可能会崩溃 如果无法找到或访问文件,则会出现分段错误。
- 在 C 中,不同于 0 或 NULL 的所有内容都为真。那适用于 关于数值的指针。随意使用否定 操作员 ”!”结合这样的测试。