//mm.h header file
# Including
# Including
# define mu _ num _ max 100// Suppose there are 100 kinds of dishes.
# defineLENsizeof(structMenuInfo)
structMenuInfo
{
intID
char menu name[20];
Floating price;
} Menu[Menu _ NUM _ MAX];
/* Basic data maintenance */
voidAddMenu()
{
FILE * fp
intmenu _ num
Printf ("How many dishes do you want to add? :");
scanf("%d ",& ampmenu _ num);
for(inti = 0; I {
printf(" "); //addedthisline
Printf ("Please enter ID:");
Scanf("%d ",& menu [i]. ID);
Printf ("Please enter the name of the dish:");
Scanf("%s ",menu [i]. MenuName);
Printf ("Please enter the price of [%s]:", menu [i]. Menu);
Menu [i]. Price = 0.0f//initialfloatprice
Scanf("%f ",& menu [i]. Price);
fflush(stdin);
}
if((fp=fopen("MenuInfo.dat "," ab"))==NULL)//openbinaryfile
{
printf(" Can ' to penfile ");
Exit (1);
}
for(intj = 0; j{
if(fwrite(& amp; Menu[j],LEN, 1,fp)! = 1)// Write data
printf("Errorwritingfile。" );
}
fclose(FP); //closefilepoint
}
void()
{
FILE * fp
printf(" ID name price "); //column headings
if((fp=fopen("MenuInfo.dat "," Rb ")= = NULL)//openbinaryfile
{
printf(" Can ' to penfile ");
Exit (1);
}
inti = 0;
do
{
fseek(fp,i*LEN,SEEK _ SET); //movefileheadlocation
if(fread(& amp; Menu[i],LEN, 1,FP))//readdatasavetostructurevariable
{
Printf ("%d% 5s% 5. 1f yuan ",menu [i]. ID, menu [i]. Menu [i]. Price);
Me;
}
}while(! feof(FP));
fclose(FP);
}
void()
{
FILE * fp
intMenuID
intto delete =- 1;
inti = 0;
Printf ("Please enter the ID of the dish to be deleted:");
scanf("%d ",& ampMenuID);
/* loadorreloadthefilendcheckthatrecord with atidexists */
if((fp=fopen("MenuInfo.dat "," Rb ")= = NULL)//openbinaryfile
{
printf(" Can ' to penfile ");
Exit (1);
}
do
{
fseek(fp,i*LEN,SEEK _ SET); //movefileheadlocation
if(fread(& amp; Menu[i],LEN, 1,fp))
{
If (menu [i]. ID = = MenuID)to delete = I;
Me;
}
}while(! feof(FP));
fclose(FP);
if(todelete==- 1)
{
printf(" AmenuwiththatIDdoesn ' texist ");
}
other
{
/* writerecordsbacktofileexcluding onetobedeleted */
if((fp=fopen("MenuInfo.dat "," WB ")= = NULL)//openbinaryfile
{
printf(" Can ' to penfile ");
Exit (1);
}
for(intj = 0; j
{
If(j==todelete) continues; /* skipercordtobedeleted */
if(fwrite(& amp; Menu[j],LEN, 1,fp)! = 1)// Write data
printf("Errorwritingfile。" );
}
fclose(FP); //closefilepoint
}
}
voidFindMenu()
{
FILE * fp
intMenuID
boolfind _ mark = false
Printf ("Please enter the name ID of the dish you are looking for:");
scanf("%d ",& ampMenuID);
printf(" ID name price "); //column headings
if((fp=fopen("MenuInfo.dat "," Rb ")= = NULL)//openbinaryfile
{
printf(" Can ' to penfile ");
Exit (1);
}
inti = 0;
do
{
fseek(fp,i*LEN,SEEK _ SET); //movefileheadlocation
Fred (& ampMenu[i], LEN, 1, fp); //readdatasavetostructurevariable
If (menu [i]. ID==MenuID)
{
Printf ("%d% 5s% 5. 1f yuan ",menu [i]. ID, menu [i]. Menu [i]. Price);
find _ mark = true
Break;
}
Me;
}while(! feof(FP));
If (! Find_mark)printf ("Dear customers, we don't have what you want in our restaurant. You can try our specialty. ”);
fclose(FP);
}
/* Basic data has been maintained */
//sc.cpp master file
# Including
# Including
# including "mm.h"
Voidmain (invalid)
{
//AddMenu();
//();
//find menu();
}