|
楼主 |
发表于 2004-3-8 03:09:06
|
显示全部楼层
已经解决了。谢谢默难老大了。现更正如下:
(不会又挑刺吧)
[code:1]
#include <stdio.h>
#include <string.h>
#define MAX 10 /*设置密码长度*/
void setpassword(void); /*设置密码函数*/
void password(void); /*检验密码函数*/
void function(void);
int main (int argc, char *argv[]) /*主函数*/
{
FILE *CheckFile;
if ((CheckFile = fopen("password.dat", "rb")) == NULL)
{
printf("\n发现程序密码为空,本程序需要输入密码才能运行!\n");
setpassword();
}
else
password();
}
void setpassword(void)
{
FILE *SetPasswordFile;
char SetPassword[MAX];
char ConfirmPassword[MAX];
char AddPass[MAX],*pAddPass = AddPass;
char Input;
int Count = 0; /*同下*/
memset (SetPassword, 0, MAX); /*数组设置初值,不设置会产生段错误*/
memset (SetPassword, 0, MAX);
memset (AddPass, 0, MAX);
system ("stty -echo"); /*屏蔽屏幕输出*/
printf("\n请输入密码:");
while ((Input = getchar ()) != '\n')
{
SetPassword[Count] = Input;
Count++;
}
SetPassword[Count] = '\0';
if (Count > MAX)
{
printf("\n密码过长!!\n");
system("stty -cbreak echo"); /*恢复屏幕输出*/
exit (-1);
}
Count = 0;
printf("\n请确认密码:");
while ((Input = getchar ()) != '\n')
{
ConfirmPassword[Count] = Input;
Count++;
}
ConfirmPassword[Count] = '\0';
if ((strcmp (SetPassword, ConfirmPassword)) == 0)
printf("\n密码符合! :)\n");
else
{
printf("\n密码不符合! :(\n");
system("stty -cbreak echo");
exit (0);
}
if ((SetPasswordFile = fopen ("password.dat", "wb")) == NULL)
{
fprintf(stderr,"\n系统错误! @_@\n"); /*stderr是标准错误输出设备*/
system("stty -cbreak echo");
exit(-1); /*程序异常退出是-1,正常为0*/
}
else
printf("\n密码设置成功! ^_^\n");
Count =0;
while ((SetPassword[Count]) != '\0') /*简单加密*/
{
AddPass[Count] = SetPassword[Count] +3;
Count++;
}
system ("stty -cbreak echo");
fwrite (pAddPass, sizeof(char), MAX, SetPasswordFile);
fclose (SetPasswordFile);
}
void password(void)
{
FILE *GetPasswordFile;
char GetPassword[MAX], *pGetPassword[MAX];
char ComparePassword[MAX];
char SubPass[MAX], *pSubPass = SubPass;
char Input;
int Count = 0;
int Times = 0;
memset(GetPassword, 0, MAX);
memset(ComparePassword, 0, MAX);
memset(SubPass, 0, MAX);
if ((GetPasswordFile = fopen ("password.dat", "rb")) == NULL)
{
fprintf(stderr, "\n密码没有设置! 6_6\n");
exit (-1);
}
fread (pSubPass, sizeof(char), MAX, GetPasswordFile);
while ((SubPass[Count]) !='\0') /*简单解密*/
{
GetPassword[Count] = SubPass[Count] -3;
Count++;
}
while (Times < 3) /*设置密码出错次数*/
{
memset(ComparePassword, 0, MAX);
Count = 0;
system("stty -echo");
printf("\n请输入您的密码:");
while ((Input = getchar ()) != '\n')
{
ComparePassword[Count] = Input;
Count++;
}
ComparePassword[Count] ='\0';
if ((strcmp (GetPassword, ComparePassword)) == 0)
{
printf("\n密码正确!\n");
system("stty -cbreak echo");
printf("\n谢谢您的使用!. by lluct @^_^@\n");
function();
exit (0);
}
else
printf("\n密码错误!!! *_*\n");
Times++;
}
system ("stty -cbreak echo");
fclose (GetPasswordFile);
}
void function(void)
{
int choose;
printf("%s\n%s",
"\n1:删除密码 2:打开光驱 3:关闭光驱 4:退出\n",
"请选择:");
scanf("%d", &choose);
switch (choose)
{
case 1:
{
system("rm password.dat");
printf("密码已删除,请重新运行程序设置密码!\n");
break;
}
case 2:
system ("eject -r&");
break;
case 3:
system ("eject -t&");
break;
case 4:
exit (0);
default:
printf("选择错误!!\n");
}
}
[/code:1] |
|