gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:48:28 29 4
gpt4 key购买 nike

我想在文件中搜索密码和标识号。我将它们完美地写入文件中,但是每当我尝试搜索它们时,每当我运行/编译代码时,屏幕上都不会弹出任何内容,我不知道出了什么问题。我真的很感激一些帮助。

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. 代码可以干净地编译
  2. 代码非常可读”
  3. 问题中的所有评论均已得到解决

现在是代码:

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

关于c - 尝试从文件中搜索标识号和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941759/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com