#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
clrscr();
char username[15], password[15], ch, usr[15], pass[15];
int choice, i=0, found=0;
printf("1.Add Admin");
printf("\n2.Log-in Admin");
printf("\n3.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: FILE *acc;
if((acc = fopen("c:\\account.txt","a+")) == NULL)
printf("FILE NOT FOUND!");
else
{
do
{
fscanf(acc,"%s %s%",username, password);
}while(!feof(acc));
}
printf("\nEnter desired username: ");
scanf("%s",username);
printf("Enter desired password: ");
while(ch != 13)
{
ch = getch();
password[i] = ch;
i++;
printf("*");
}
password[i]='\0';
fprintf(acc,"%s %s\n",username,password);
fclose(acc);break;
case 2: FILE *log;
log = fopen("c:\\account.txt","r");
printf("Username: "); //user input username and password//
scanf("%s",usr);
printf("Password: ");
while(ch != 13)
{
ch = getch();
pass[i] = ch;
i++;
printf("*");
}
pass[i]='\0';
while(!feof(log)&& found == 0)
{
fscanf(log,"%s %s",username,password); //this is where i am having some problem//
if(strcmp(usr,username) == 0 && strcmp(pass,password));
found = 1;
if(found == 1)
printf("\nWelcome!");
else
printf("\nInvalid username or password");
}
fclose(log);
}
getch();
return(0);
}
我無法驗證工作,它總是說歡迎,即使它有錯誤的用戶名和密碼。 如何將用戶輸入字符串與文件中的數據進行比較? 不幸的是turbo c是必需的。用戶名和密碼驗證turbo c
你爲什麼成炭閱讀密碼字符? – Haris 2014-10-09 09:11:43
嘗試將您的代碼縮減爲顯示問題 – thumbmunkeys 2014-10-09 09:11:48
以將字符替換爲「*」的代碼片段。 – Dme12 2014-10-09 09:19:06