对不起,这样简单的问题,这是我的任务的一部分,我被困。 如你看到的
#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中被接受。
谢谢你的帮助。
编辑:更新的完整的代码,分配新建分配FY变了,单个文件需要更换,我不能使用第二个文件。
既然你已经有两个文件,同时打开这两个文件。 当你从一个读每一行,你要么相同的数据到另一个,或新的数据写入到其他,这取决于用户的选择。
FILE *update = fopen("menu2.txt", "r");
FILE *menu = fopen("/home/mbp/menu.txt","w+");
for (...) {
fscanf(update, ...);
if (user_wants_update()) {
get_new_info(...);
fprintf(menu, ...); /* print the new info */
} else {
fprintf(menu, ...); /* print the old info */
}
}
fclose(menu);
fclose(update);
的“不工作”的问题将有更详细的受益,因为在如何颂歌它不能正常工作。 这是我最好的拍摄。
改变“%C”到“%C”
操作码混合scanf("%s",...
用scanf("%c",...
。这是一个问题,如果之前scanf("%c",...
,在尚未发布的代码的某个地方,你执行scanf("%s",...
等等。
在scanf("%s",buf)
消耗所有前导空格,然后把下面的非白色文本buf
, 留下了“输入”( \n
输入缓冲区)。 阿继scanf("%c",...
将读取( \n
),甚至迫不及待来回您键入类似y
。通过改变“%C”到“%C”,即( \n
)和额外的空白将被消耗(扔), 然后你的y
将得到扫描。
此外,考虑检查scanf()的和的fscanf的返回值()。 这将certianly帮助您调试代码。
您需要做的比扫描和打印等等。 这里是你的一些伪代码:
read in line from file 1
check if line needs modification
if so
modify line
write line to file 2
下面是一个简单的例子程序
#include <stdio.h>
#include <string.h>
int main()
{
FILE *f1 = fopen("1.txt", "r");
FILE *f2 = fopen("2.txt", "w");
char line[50];
while (fscanf(f1, "%s", line) != EOF)
{
if (strcmp(line, "replaceme") == 0)
{
strcpy(line, "replaced");
}
fprintf(f2, "%s", line);
}
fflush(f2);
fclose(f1);
fclose(f2);
}
可能这可以工作。 我固定在两个错误你的代码。
update = fopen("menu2.txt","r");// you open a FILE and give the fileid to updata
for(j=0;j<kk;j++) {
fscanf(update,"%s %d %d\n",name,&mealnum,&price);
//so you have a file ,already writed format things.
printf("Update %s %d %d?\n Y to update any other key for next",name,mealnum,price);
scanf("%c\n",&updateid);
//I think it's better use "%c\n", then you can know it not stay in buffer.
if(updateid == 'Y') //as we print, 'Y' to update ..
break;// if you can not use goto , don't use.//goto out;
}
//out:
// I believe you already declare all those values.
printf("Enter the name of meal\n");
scanf("%s",&name);
printf("Enter the ID of meal\n");
scanf("%d",&id);
printf("Enter the Price of meal\n");
scanf("%d",&price);
fclose(update);// close the (FILE * ) update. In fact, I think here is you mistake.
menu = fopen("/home/mbp/menu.txt","w+");//menu is used just now.
for(d=0;d<j-1;d++) {
// fscanf(menu,"%s %d %d\n",name,mealnum,price);
//here ,you overwrite you values. All you input is missing; Here is another mistake.
int mealnum1,price1;
char name1[10];//I don't know the size... :)
fscanf(menu, %s %d %d\n",&name1,&mealnum1,&price1);
}
fprintf(menu,"%s %d %d\n",name,mealnum,price);