用fprintf和fscanf替换文本文件中的字符串

时间:2013-08-03 01:25:45

标签: c scanf printf

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

#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 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

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

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

OP代码将scanf("%s",...scanf("%c",...混合。如果在scanf("%c",...之前,在未发布的代码中某处,您执行了scanf("%s",...之类的操作,则会出现此问题。

scanf("%s",buf)消耗所有前导空格,然后将以下非白色文字放入buf离开“输入”(\n)输入缓冲区。然后,以下scanf("%c",...将会读取(\n),甚至不会等到您输入类似y的内容。通过将“%c”更改为“%c”,(\n)和其他空格将被消耗(并被抛出),然后将会扫描您的y

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

答案 2 :(得分: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);
}

答案 3 :(得分: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);