2014-10-09 161 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