2014-10-09 139 views
-2
#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

+0

你为什么成炭阅读密码字符? – Haris 2014-10-09 09:11:43

+1

尝试将您的代码缩减为显示问题 – thumbmunkeys 2014-10-09 09:11:48

+0

以将字符替换为“*”的代码片段。 – Dme12 2014-10-09 09:19:06

回答

0

的当前密码输入包括密码换行符,当通过fscanf检索密码时,新行将被忽略。
试试这个密码输入密码都输入

printf("Enter desired password: "); 
i = 0; // to be safe set i to zero       
while((ch = getch()) != '\n') // newline will be detected here and not added to password 
{      
    password[i] = ch; 
    i++;   
    printf("*"); 
    if (i >= 14) { // to prevent a long password overwriting the array 
     break; 
    } 
}           
password[i]='\0'; 

编辑:
的“\ n”在Linux和海湾合作委员会的工作对我罚款。对于turbo c,你可能不得不使用'\ n',因为你必须开始,或者'\ r'。
如果在文件中多套的用户名/密码,您将看到每个设置的欢迎/无效的消息作为该消息是while循环从文件中读取里面。
试试这个从文件中读取和验证用户名/密码

found = 0; 
while((fscanf(log," %s %s",username,password == 2) { 
{    
    if(strcmp(usr,username) == 0 && strcmp(pass,password) == 0) 
    {   
     found = 1; 
     break; 
    }   
}    
if(found == 1) // moved outside while loop so it is seen only once 
    printf("\nWelcome!"); 
else   
    printf("\nInvalid username or password"); 
+0

它有点作品,但是如果我想通过在密码输入过程中按回车停止?它也显示无效的消息2次,如果无效的usrname和密码。 – Dme12 2014-10-09 13:40:42

+0

哇!你做了我的一天男人!感谢您的大力帮助!上帝保佑! – Dme12 2014-10-10 00:04:25

0

在你的代码,当你比较在密码strcmp的用户名和密码

if(strcmp(usr,username) == 0 && strcmp(pass,password)); 

,检查它是否已经返回0或不丢失。试试这个

if(strcmp(usr,username) == 0 && strcmp(pass,password) == 0) 

而且,你if循环有中端;,这意味着found = 1不是if循环中,取出;

+0

我确实在0的地方,但仍然做同样的事情。 – Dme12 2014-10-09 09:17:48

+0

@ Dme12:编辑答案,检查... – Haris 2014-10-09 09:20:18

+0

噢!但现在它只是显示无效的用户名或密码。 – Dme12 2014-10-09 09:23:04