2017-03-22 3 views
0

私はファイルにパスワードと識別番号を検索したいと思います。私はファイルに完全にそれらを書いたが、私はいつでも私はそれらを検索しようとしたときに何も動作しない/コードをコンパイルする何も何が間違っているのかわからない画面上のポップアップ。私は本当にいくつかの助けに感謝します。ここファイルから識別番号とパスワードを検索しようとしています

void EgleEnergyEmployee() 
{ 
    FILE *fp = NULL; 
    int Employ_Iden,i; 
    char Employ_Name[30],Password[30],Username[30],Job_Type[30]; 

    fp = fopen("Employee Data.txt","w"); 
    if (fp==NULL) 
    { 
     printf("File is not available\n"); 
    } 
    else 
     { 
      printf("Please enter identification number\n"); 
      scanf("%d", &Employ_Iden); 
      printf("Please enter your name\n"); 
      scanf("%s", Employ_Name); 
      printf("Please enter your Job Title\n"); 
      scanf("%s", Job_Type); 
      printf("Please enter your username\n"); 
      scanf("%s", Username); 
      printf("Please enter password\n"); 
      scanf("%s", Password); 
      while (!feof(stdin)) 
      { 
       fprintf(fp,"Employee Identification Numbers %10d Employee Name %10s Username %10s Password %10s\n",Employ_Iden,Employ_Name,Job_Type,Username,Password); 
       printf("Please enter identification number\n"); 
       scanf("%d", &Employ_Iden); 
       printf("Please enter your name\n"); 
       scanf("%s", Employ_Name); 
       printf("Please enter your Job Title\n"); 
       scanf("%s", Job_Type); 
       printf("Please enter your username\n"); 
       scanf("%s", Username); 
       printf("Please enter password\n"); 
       scanf("%s", Password); 
      } 
     } 
     fclose(fp); 

    int SearchEmployee(int EmployID) 
    { 
     FILE *fp = NULL; 
     bool NotFound = 1; 
     int Employ_Iden,i; 
     char Employ_Name[30],Password[30],Username[30],Job_Title[30]; 

     fp = fopen("Employee Data.txt","r"); 
     if (fp==NULL) 
     { 
      printf("File is not available\n"); 
     } 
     else 
      { 
       rewind(fp); 
       while (!feof(fp)&& NotFound) 
       { 
        fscanf(fp,"%d %s %s %s",&Employ_Iden,Employ_Name,Password,Username); 
        if (Employ_Iden == EmployID) 
        { 

         printf("%d \t %s \t %s \t %s",Employ_Iden,Employ_Name,Password,Username); 
         NotFound = 0; 
        } 
        else 
         { 
          NotFound = 1; 
          //printf("Not Found\n"); 
         } 
       } 
      } 
      fclose(fp); 
     } 
    int main() 
    { 
       int result,Employ; 

       printf("Please enter your employee identification number\n"); 
       scanf("%d", &Employ); 
       result = SearchEmployee(Employ); 
       if (result == Employ) 
       { 
        printf("Its here"); 
       } 
       else 
       { 
        printf("Its not here"); 
       } 
+0

コメントは拡張された議論のためのものではありません。この会話は[チャットに移動]されています(http://chat.stackoverflow.com/rooms/138795/discussion-on-question-by-a-tait-trying-to-search-for-a-identification-number- )。 –

答えて

0

は、コードを書くための一つの方法である:

  1. コードがきれいに問題になっているすべてのコメントを

に対処している

  • コードは非常に読みやすいコンパイル今度はコード:

    #include <stdio.h> // fread(), fwrite(), scanf(), printf() 
    #include <stdlib.h> // exit(), EXIT_FAILURE 
    
    
    #define MAX_LEN 30 
    
    struct data 
    { 
        int Employ_Iden; 
        char Employ_Name[ MAX_LEN ]; 
        char Password[ MAX_LEN ]; 
        char Username[ MAX_LEN ]; 
        char Job_Type[ MAX_LEN ]; 
    }; 
    
    
    void EgleEnergyEmployee(void); 
    int SearchEmployee(int EmployID); 
    
    
    void EgleEnergyEmployee(void) 
    { 
        FILE *fp = NULL; 
        struct data Employee; 
    
        fp = fopen("Employee Data.txt","a"); 
        if (fp==NULL) 
        { 
         perror("File is not available\n"); 
         exit(EXIT_FAILURE); 
        } 
    
        while(1) 
        { 
         printf("Please enter identification number or -1 to stop\n"); 
         if(1 != scanf("%d", &Employee.Employ_Iden)) 
         { 
          perror("scanf for employee ID failed"); 
          fclose(fp); 
          exit(EXIT_FAILURE); 
         } 
    
         // implied else, scanf successful 
    
         if(-1 == Employee.Employ_Iden) 
         { 
          break; 
         } 
    
         printf("Please enter your name\n"); 
         if(1 != scanf("%29s", Employee.Employ_Name)) 
         { 
          perror("scanf for employee name failed"); 
          fclose(fp); 
          exit(EXIT_FAILURE); 
         } 
    
         // implied else, scanf successful 
    
         printf("Please enter your Job Title\n"); 
         if(1 != scanf("%29s", Employee.Job_Type)) 
         { 
          perror("scanf for employee job type failed"); 
          fclose(fp); 
          exit(EXIT_FAILURE); 
         } 
    
         // implied else, scanf successful 
    
         printf("Please enter your username\n"); 
         if(1 != scanf("%29s", Employee.Username)) 
         { 
          perror("scanf for employee user name failed"); 
          fclose(fp); 
          exit(EXIT_FAILURE); 
         } 
    
         // implied else, scanf successful 
    
         printf("Please enter password\n"); 
         if(1 != scanf("%29s", Employee.Password)) 
         { 
          perror("scanf for employee password failed"); 
          fclose(fp); 
          exit(EXIT_FAILURE); 
         } 
    
         // implied else, scanf successful 
    
         if(1 != fwrite(&Employee, sizeof(Employee), 1, fp)) 
         { 
          perror("fwrite for new employee record failed"); 
          fclose(fp); 
          exit(EXIT_FAILURE); 
         } 
        } // end while 
    
        fclose(fp); 
    } // end function: EgleEnergyEmployee 
    
    
    int SearchEmployee(int EmployID) 
    { 
        FILE *fp = NULL; 
        struct data Employee; 
    
        fp = fopen("Employee Data.txt","r"); 
        if (fp==NULL) 
        { 
         perror("File is not available\n"); 
         exit(EXIT_FAILURE); 
        } 
    
    
        while( 1 == fread(&Employee, sizeof(Employee), 1, fp)) 
        { 
         if (Employee.Employ_Iden == EmployID) 
         { 
          printf("%d \t %s \t %s \t %s", 
           Employee.Employ_Iden, 
           Employee.Employ_Name, 
           Employee.Password, 
           Employee.Username); 
          return EmployID; // indicate search successful 
          break; 
         } 
        } 
    
        fclose(fp); 
    
        return 0; // indicate search failed 
    } // end function: SearchEmployee 
    
    
    int main(void) 
    { 
        int result; 
        int Employ; 
    
        printf("Please enter your employee identification number\n"); 
        if(1 != scanf("%d", &Employ)) 
        { 
         perror("scanf for your employee number failed"); 
         exit(EXIT_FAILURE); 
        } 
    
        // implied else, scanf successful 
    
        result = SearchEmployee(Employ); 
    
        if (result == Employ) 
        { 
         printf("Its here"); 
        } 
    
        else 
        { 
         printf("Its not here"); 
        } 
    } 
    

    注:関数:EgleEnergyEmployee()はまだ呼び出されていません。

  • +0

    本当にありがとうございました。 –

    関連する問題