抱歉这么简单的问题,这是我任务的一部分,我被困住了。如你看到的
#include <stdio.h>
int main (void){
FILE *menu;
FILE *update;
FILE *updatewrite;
menu = fopen("menu.txt","w");
char selection,name[10],updateid,dump ;
int mealnum,i,j,id,price ;
start:
scanf("%c%c",&selection,&dump);
if (selection =='r'){
printf ("Enter the number of meals to be entered\n");
scanf("%d",&mealnum);
for(i=0;i<mealnum;i++){
printf("Enter the name of me1al\n");
scanf("%s",&name);
printf("Enter the ID of meal\n");
scanf("%d",&id);
printf("Enter the Price of meal\n");
scanf("%d",&price);
fprintf(menu,"%s %d %d\n",name,id,price);
}
fclose(menu);
}
else if(selection =='u'){
update = fopen("menu.txt","r");
int count=0;
while(fscanf(update,"%s %d %d\n",name,&mealnum,&price) != EOF){
printf("Update %s %d %d?\n Y to update any other key for next",name,mealnum,price);
scanf("%c",updateid);
count++;
break;
}
printf("Enter the new name of meal\n");
scanf("%s",name);
printf("Enter the new ID of meal\n");
scanf("%d",&id);
printf("Enter the new Price of meal\n");
scanf("%d",&price);
fclose(update);
updatewrite = fopen("/home/mbp/menu.txt","w+");
for(j=0;j<count;j++){fscanf(updatewrite,"%s %d %d\n",name,mealnum,price);} //trying to move buffer to proper overwriting location by looping one less times
fprintf(updatewrite,"%s %d %d\n",name,mealnum,price);
fclose(updatewrite);}
else if(selection =='d'){}
else if(selection =='s'){}
else if(selection =='b'){}
else if(selection =='q'){
return 0;
}
else{printf ("Not VALID!");}
goto start;
return 0; }
除了 fscanf,fprintf 也被接受。
谢谢你的帮助。
编辑:更新完整代码,更改分配,需要替换单个文件,我不允许使用第二个文件。