当前位置:首页 » 编程语言 » c语言设计登录密码程序
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言设计登录密码程序

发布时间: 2022-05-26 04:52:31

㈠ 如何使用c语言编写一个密码程序

密码保存在文件中,从文件中读取密码,但是没做容错和异常处理,仅供参考

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

#define PSDLEN 6

void inputPsd(char *str) /*处理输入*/
{
int i;

for(i = 0; i < PSDLEN; i++)
{
while(1)
{
str[i] = getch();
if(str[i] == '\b') /*处理退格键*/
{
i--;
if(i < 0)
{
i = 0;
}
else
{
printf("\b \b");
}
continue;
}
else if(str[i] == '\r') /*处理回车键*/
{
continue;
}
else
{
printf("*");
break;
}
}
}
str[i] = '\0';
printf("\n");
}

int checkFirst() /*检测是否是第一次使用*/
{
FILE *fp;
if((fp = fopen("psd.dat", "rb")) == NULL)
{
return 1;
}
fclose(fp);
return 0;
}

void firstUse() /*第一次使用 需要输入密码*/
{
FILE *fp;
int i;
char passwd[PSDLEN + 1];
char checkPsd[PSDLEN + 1];

if((fp = fopen("psd.dat", "wb")) == NULL)
{
printf("Creat password error!\n");
exit(1);
}
while(1)
{
printf("Please input password:");
inputPsd(passwd);

printf("\nPlease input password again:");
inputPsd(checkPsd);

if(!strcmp(passwd, checkPsd))
{
break;
}
printf("\ncheck password error! \n");
}
fwrite(passwd, sizeof(char), PSDLEN, fp);
fclose(fp);
}

void login() /*核对密码,并登录*/
{
FILE *fp;
int i, num = 3;
char passwd[PSDLEN + 1];
char checkPsd[PSDLEN + 1];

if((fp = fopen("psd.dat", "rb")) == NULL)
{
puts("Open psd.dat error");
exit(1);
}
fread(passwd, sizeof(char), PSDLEN, fp);
fclose(fp);
passwd[PSDLEN] = '\0';

printf("Please input password to login");
while(num)
{
printf("you have %d chances to cry:\n", num);
inputPsd(checkPsd);
if(!strcmp(passwd, checkPsd))
{
break;
}
puts("\npassword error,Please input again");
num--;
}
if(!num)
{
puts("Press any key to exit...");
getch();
exit(0);
}
else
{
puts("\n--------\nWelcome!\n--------\n");
}
}

void main()
{
if(checkFirst())
{
firstUse();
}
else
login();

getch();
}

㈡ 怎么用c语言写一个创建用户名和密码并且能修改密码的程序

#include <stdio.h>//我自己做的,你拿去用吧!
#include <string.h>
struct e
{
char a[10];
char b[10];
}z;
int main()
{ int t=0;
char s[10],d[10];
FILE *p;
void as();
if ((p=fopen("m.txt","r+"))==NULL)
{
p=fopen("m.txt","w+");
t=1;
}
if(t==1)
{
printf("当前没有任何用户\n");
printf("请新建用户名: ");
scanf("%s",s);
printf("为用户设置密码: ");
scanf("%s",d);
strcpy(z.a,s);
strcpy(z.b,d);
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
}
if(t==0)
{
printf("请输入用户名: ");
scanf("%s",s);
fscanf(p,"%s %s",z.a,z.b);
fclose(p);
if (!strcmp(z.a,s))
{
printf("请输入密码:");
scanf("%s",d);getchar();
if(!strcmp(z.b,d))
{ char i;
printf("是否要修改密码?(输入y修改,n退出!)");
scanf("%c",&i);
if(i=='y')
{
printf("请输入修改密码:");
scanf("%s",z.b);
p=fopen("m.txt","w+");
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
printf("修改成功!");

}

}
else printf("密码错误!");
}
else printf("用户名错误");
fclose(p);

}

}

㈢ C语言编写用户登录程序

艾达的小刀
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

/*随机码产生函数*/
void RandomCode (char Rcode[])
{
int i;
srand ((unsigned int)time(NULL));
for (i = 0; i < 3; ++i)
Rcode[i] = rand()%10 + '0';
Rcode[i] = '\0';
}
/*登陆函数,判断信息是否匹配,若匹配返回1,否则返回0*/
int LandedApp (char *password[], char Rcode[])
{
char name[10] = {0};
char pword[10] = {0};
char rcode[4] = {0};

printf ("用户名 : ");
gets (name);
printf ("密码 : ");
gets (pword);
printf ("随机码 : ");
gets (rcode);

if (strcmp (name, password[0]) != 0 || strcmp (pword, password[1]) != 0 || strcmp (rcode, Rcode) != 0)
return 0;
else
return 1;
}

int main ()
{
char * password[2] = {"admin", "admin123"}; //用户名和密码
char rc[4] = {0}; //随机码
int count = 3; //可输入次数

puts ("请输入用户名,密码和随机码:");
while (count)
{
RandomCode (rc);
printf ("随机码 : %s\n", rc);
if (LandedApp(password, rc) != 0)
break;
--count;
if (count != 0)
puts ("错误的用户名或密码或随机码,请重新输入: ");
}
if (count != 0)
puts ("\n成功登陆!");
else
puts ("\n登录失败 !");

return 0;
}
艾达的小刀

㈣ C语言密码程序设计

手写的,没运行,你自己试一下

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
intmain()
{
char st[6];
printf("输入登陆密码: ");
gets(st);
if(strcmp(st,"123")==0)
printf("7");//响铃
else
printf("Error ");
return(0);
}

㈤ c语言程序设计 密码设置程序怎么编写

- -|
这么多问题才10分...
给你一个密文输出的程序好了..
其他自己想.
#include <stdio.h>
main()
{
char str[9]; //密码串长9为
inputPW(str,8); //有效密码长为8 最后一位要放\0 结束符的!
printf("\n密码为: %s",str);
}

inputPW(char * s,int len) //自己写的密码输入的函数
{
int i;
fflush(stdin); //清输入流 跟flushall()类似
for(i=0; ;i++)
{
s[i]=getch();
if(s[i]==13) //输入结束 不能用=='\n'来判断!!
//因为对于回车来说getchar()='\n'=10;而getch()=13 !='\n' 这个要知道!
break;
if(s[i]==8 && i>0) //如果用户按退格键 并且要有格可退时候
{
printf("\b \b"); //显示退一格
i=i-2; //输入数据退2(因为for循环体会加1 所以实际就是退了1)
continue;
}
if(i==len) {i--; continue;}
printf("*");
}
s[i]='\0'; //末尾补\0 所以该密码实际有效长度为i-1;定义有效长为len 实际定义的串长为len+1
}

㈥ C语言用c写一个可以验证账号,密码和修改密码的程序

#include <string.h>
struct e
{
char a[10];
char b[10];
}z;
int main()
{ int t=0;
char s[10],d[10];
FILE *p;
void as();
if ((p=fopen("m.txt","r+"))==NULL)
{
p=fopen("m.txt","w+");
t=1;
}
if(t==1)
{
printf("当前没有任何用户\n");
printf("请新建用户名: ");
scanf("%s",s);
printf("为用户设置密码: ");
scanf("%s",d);
strcpy(z.a,s);
strcpy(z.b,d);
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
}
if(t==0)
{
printf("请输入用户名: ");
scanf("%s",s);
fscanf(p,"%s %s",z.a,z.b);
fclose(p);
if (!strcmp(z.a,s))
{
printf("请输入密码:");
scanf("%s",d);getchar();
if(!strcmp(z.b,d))
{ char i;
printf("是否要修改密码?(输入y修改,n退出!)");
scanf("%c",&i);
if(i=='y')
{
printf("请输入修改密码:");
scanf("%s",z.b);
p=fopen("m.txt","w+");
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
printf("修改成功!");
}
}
else printf("密码错误!");
}
else printf("用户名错误");
fclose(p);
}
}

㈦ c语言编写用户登录程序

代码如下:

#include<stdio.h>

#pragma warning(disable:4996)

#include<string.h>

int main()

{

int i = 0;

char password[10] = { 0 };

printf("请输入密码:");

while (i < 3)

{

scanf("%s", password);

printf(" ");

if (strcmp(password, "972816") == 0)

{

printf("登录成功 ");

break;

}

else

{

i++;

if (i != 3)

printf("再输入一次");

}

}

if (i == 3)

printf("密码错误三次退出登录界面 ");

system("pause");

return 0;

(7)c语言设计登录密码程序扩展阅读:

#include后面有两种方式,<>;和""前者先在标准库中查找,查找不到在path中查找。后者为文件路径,若直接是文件名则在项目根目录下查找。

引用方法:#include<stdio.h>

注意事项:在TC2.0中,允许不引用此头文件而直接调用其中的函数,但这种做法是不标准的。也不建议这样做。以避免出现在其他IDE中无法编译或执行的问题。

㈧ 用C语言设计一个模拟程序,完成计算机登录时的密码验证过程。

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

intmain(intargc,char*argv[])
{
constchar*password="123456";
charbuff[64]={0};

printf("请输入需要校验的密码: ");
scanf("%s",buff);//如果要考虑空格使用fgets

if(0==strcmp(password,buff))
{
printf("输入正确 ");
}
else
{
printf("输入错误 ");
}

return0;
}

㈨ 如何用C语言编写密码程序

1、用一个字符数组来存密码
再用一个字符数组接收你的输入,然后用strcmp
来比较,如果返回0则密码是正确的
2、例程:

#include"stdio.h"
#include"string.h"
intmain()
{
charmima[100]="YuanShi888";
charinput[100]={0};
printf("请输入密码:");
gets(input);
if(strcmp(mima,input)==0)
printf("恭喜你,密码正确! ");
else
printf("对不起,密码输入错误! ");

}

㈩ C语言编写一个用户登陆的程序

代码如下:

#include<stdio.h>

#pragma warning(disable:4996)

#include<string.h>

int main()

{

int i = 0;

char password[10] = { 0 };

printf("请输入密码:");

while (i < 3)

{

scanf("%s", password);

printf(" ");

if (strcmp(password, "972816") == 0)

{

printf("登录成功 ");

break;

}

else

{

i++;

if (i != 3)

printf("再输入一次");

}

}

if (i == 3)

printf("密码错误三次退出登录界面 ");

system("pause");

return 0;

(10)c语言设计登录密码程序扩展阅读:

#include后面有两种方式,<>;和""前者先在标准库中查找,查找不到在path中查找。后者为文件路径,若直接是文件名则在项目根目录下查找。

引用方法:#include<stdio.h>

注意事项:在TC2.0中,允许不引用此头文件而直接调用其中的函数,但这种做法是不标准的。也不建议这样做。以避免出现在其他IDE中无法编译或执行的问题。