2012-10-16 70 views
0

我正在嘗試製作一個記錄系統,其中我要登錄,使用我的員工ID和密碼登錄,或者如果可能,只需要員工ID。問題是,無論何時我將僱主ID放入一個文本文件中,我的代碼僅限於我的代碼中預定義的用戶名。我如何讓每個僱主的身份證明文件都能被接受並能成功登錄?C程序 - 文件處理限制

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

    void mainMenu(); 
    void EnterItem(); 
    void ViewItem(); 
    void SearchItem(); 
    void Exit(); 

    int count=1,ulength,plength; 
    char username[]="Admin",password[]="1234"; 
    char name1[10],pass[10]; 

    char code[20],name[20],search[20]; 
    int price,qty; 
    main(){ 

    printf("\nEnter USer ID And Password below(You have Only Three Chances!\nPress Enter To Continue"); 
     getch(); 
     while(count<=3) 
     { 

     printf("\nENter User ID:"); 
     scanf("%s",name1); 
     printf("\nENter Password:"); 
     scanf("%s",pass); 
     ulength=strcmp(name1,username); 
     plength=strcmp(pass,password); 
     if(ulength==0&&plength==0) 
     { 
     printf("\nWelcome %s",name1); 
     break; 
     } 
     else 
     { 
     printf("\nUsername And Password is Invalid!\n You Have %d more Chances/s.",3-count); 
     } 
     getch(); 
     count++; 
     } 
     if(count==4) 
     printf("Maximum of three(3)Try only!"); 
     getch(); 




     mainMenu(); 
     EnterItem(); 
     ViewItem(); 
     SearchItem(); 
     Exit(); 
     getch(); 
     } 

    void mainMenu(void){ 

    char choice; 
    printf("\t*************************************\n"); 
    printf("\t******========================*******\n"); 
    printf("\t******==!!INVENTORY SYSTEM!!==*******\n"); 
    printf("\t******========================*******\n"); 
    printf("\t*************************************\n"); 
    printf("\n"); 

    printf("\nMenu:\n[a]EnterItem\n[b]ViewItem\n[c]SearchItem\n[d]Exit\n"); 
    printf("What do you want to perform?:"); 
    fflush(stdin); 

    scanf("%c",choice); 
    switch(choice){ 

    case 'a': 
     EnterItem(); 
     break; 
    case 'b': 
     ViewItem(); 
     break; 
    case 'c': 
      SearchItem(); 
     break; 
    case 'd': 
     Exit(); 

     break; 
    default: 
     printf("Invalid Choice"); 

     } 
    } 

    void EnterItem(void){ 

    FILE *MyFile; 
    char opt; 
    do{ 
     printf("Enter Item Code:"); 
     fflush(stdin); 
     scanf("%s",code); 

     printf("Enter Item Name:"); 
     fflush(stdin); 
     scanf("%s",name); 


     printf("Enter Item Price:"); 
     fflush(stdin); 
     scanf("%i",&price); 


     printf("Enter Item Quantity:"); 
     fflush(stdin); 
     scanf("%i",&qty); 
      MyFile=fopen("MyFiles.txt","a+"); 
      fprintf(MyFile,"%s\t%s\t%i\t%i\n",code,name,price,qty); 
      printf("Item Saved!!\n"); 

     printf("Do you want to Add More?:[y/n]"); 
     fflush(stdin); 
     scanf("%c",&opt); 


    }while(opt=='Y'||opt=='y'); 
    printf("Do you want to Go Back To main?:[y/n]"); 
    fflush(stdin); 
    scanf("%c",&opt); 
    if(opt=='y'||opt=='Y') 
    mainMenu(); 
    else 
     printf("Invalid Choice!!"); 

     } 

    void ViewItem(void){ 

    FILE *MyFile; 
    int c; 

    if((MyFile=fopen("MyFiles.txt","r"))==NULL) 
    { 
    printf("Error Reading File!!"); 

    } 
    while((c=fgetc(MyFile))!=EOF) 
    printf("%c",c); 
    printf("Go to Main Menu[y/n]:"); 
    fflush(stdin); 
    scanf("%c",opt); 
    if(opt=='y'||opt=='Y') 
    mainMenu(); 
    else(); 
    fclose(MyFile); 
    getch(); 
    } 

    void SearchItem(void){ 

    FILE *MyFile; 

     MyFile=fopen("MyFiles.txt","a+"); 
     printf("Enter the Item Code to Search:"); 
     fflush(stdin); 
     scanf("%s",search); 
     while(!feof(MyFile)) 
     { 
     fscanf(MyFile,"%s %s %i %i",code,name,price,qty); 
      if(strcmp(search,code)==0) 
      { 
      printf("Item Code: %s\n",code); 
      printf("Item Name: %s\n",name); 
      printf("Item Price: %i\n",price); 
      printf("Item Quantity: %i\n",qty); 
      break; 
      printf("Go Back To main Menu[y/n]:"); 
      fflush(stdin); 
      scanf("%s",opt); 
    if(opt=='y'||opt=='Y') 
    mainMenu(); 
    else 
      } 
      } 
       fclose(MyFile); 
    } 

    void Exit(void){ 
    getch(); 
    } 
+0

完成:)剛剛編輯的代碼。 – Xelza

+0

除了在標準輸入框中調用fflush的其他錯誤,這會導致未定義的行爲。 –

回答

0

ulength=strcmp(name1,username); plength=strcmp(pass,password);

這限制了你在你的代碼只使用預定義的用戶名的能力。您正在將其與使用「Admin」的用戶名進行比較。因此,不管您輸入的用戶名是什麼,您都會將其與字符串「Admin」進行比較。

您需要做的是使用「fopen」打開包含員工詳細信息的文件。然後,您可以使用「fgets」將員工詳細信息讀入數組(或您選擇的其他數據結構)。您可以迭代列表並將其與「name1」進行比較。

乾杯, VSN

附:使用strncmp,因爲它使您的代碼更安全。如果您正在UNIX機器上工作,除了其他標準C函數外,您還將找到「fopen」,「fgets」和「strncmp」的手冊頁。

+0

等待,如果我不得不在數組中調用它,它將如何不斷讀取每個數據?我會將每個文件名存儲在數組中嗎? – Xelza