0

抱歉这么简单的问题,这是我任务的一部分,我被困住了。如你看到的

#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 也被接受。

谢谢你的帮助。

编辑:更新完整代码,更改分配,需要替换单个文件,我不允许使用第二个文件。

4

4 回答 4

1

由于您已经有两个文件,请同时打开这两个文件。当您从一行读取每一行时,您要么将相同的数据写入另一行,要么将新数据写入另一行,这取决于用户的选择。

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);
于 2013-08-03T02:04:08.007 回答
0

可能这可以工作。我修正了关于你的代码的两个错误。

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);
于 2013-08-03T05:47:09.390 回答
0

“它不起作用”的问题将受益于更多细节,例如它如何不起作用。这是我最好的镜头。

将“%c”更改为“%c”

OP 代码scanf("%s",...scanf("%c",.... 如果 scanf("%c",...在未发布代码的某处之前执行了 ascanf("%s",...或类似操作,则会出现问题。

scanf("%s",buf)消耗所有前导空白,然后将以下非白色文本放入,buf enter”(\n)留在输入缓冲区中。随后scanf("%c",...将读取 ( \n),甚至不等您输入类似y. 通过将“%c”更改为“%c”,该 ( \n) 和额外的空白将被消耗(并被丢弃),然后y将被扫描。

此外,考虑检查 scanf() 和 fscanf() 的返回值。它肯定会帮助您调试代码。

于 2013-08-03T03:10:14.967 回答
0

您需要做的不仅仅是扫描和打印。这是给你的一些伪代码:

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);
}
于 2013-08-03T03:38:02.877 回答