我有这个 C 代码,我遇到的问题是我找不到我想要的记录,使用文件处理它总是说找不到结果。使我的系统清晰运行并显示记录的正确方法是什么?
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <windows.h>
void loadMenu();
void addEmployee();
void searchEmployee();
bool found = false;
int choice;
FILE*myrec;
char Name[20],Age[20],IDnum[20],search[20];
void loadMenu(){
printf("[1]Add Employee\t[2]Search Employee\t[3]Exit\nChoice:");
scanf("%d", &choice);
}
void addEmployee (){
char ans;
do{
printf("Enter IDnumber:");
fflush(stdin);
gets(IDnum);
printf("Enter Name:");
fflush(stdin);
gets(Name);
printf("Enter Age:");
fflush(stdin);
gets(Age);
myrec = fopen ("record.txt","a+");
fprintf(myrec,"%s \t %s \t %s \n ",Name,Age,IDnum);
printf("Record Saved !");
printf("Do you want to add another record ? Y/N");
scanf("%s",&ans);
fclose(myrec);
}
while (ans=='Y'||ans=='y');
}
void searchEmployee(){
myrec = fopen("record.txt","a+");
printf("Enter Employee IDnumber:");
scanf("%s",&search);
while(!feof(myrec)){
fscanf(myrec,"%s %s %s",IDnum,Name,Age);
if (strcmp(search,IDnum)==0){
printf("IDnum: %s\n", IDnum);
printf("Name: %s\n",Name);
printf("Age :%s\n",Age);
found = true;
break;
}
}
if(!found) printf("No results.");
fclose (myrec);
}
int main(){
bool repeat = false;
do{
loadMenu();
switch(choice){
case 1:
addEmployee();
break;
case 2:
searchEmployee();
break;
case 3:
repeat = true;
break;
}
}while(!repeat);
getch();
}
我的代码有什么问题?