尝试从文件中搜索标识号和密码

时间:2017-03-22 02:43:49

标签: c visual-c++

我想在文件中搜索密码和身份证号码。我把它们完美地写到文件中,但每当我试图搜索它们时,无论何时我运行/编译代码都没有任何工作,屏幕上没有任何东西弹出我不知道什么是错的。我真的很感激一些帮助。

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");
              }

1 个答案:

答案 0 :(得分:0)

这是编写代码的一种方法:

  1. 代码干净地编译
  2. 代码非常易读“
  3. 问题中的所有评论都已解决
  4. 现在是代码:

    #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()仍未被调用