2013-08-03 86 views
0

对不起,这是我的任务的一部分,我被卡住了。正如你可以看到的用fprintf和fscanf替换文本文件中的字符串

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

感谢您的任何帮助。

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

+0

可以粘贴整个功能吗?因为这没有任何意义。 – Jocke

回答

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); 
+0

感谢您提供信息,但现在只需要一个正在读取和写入的文件 – user2647684

+0

最直接的解决方案是将整个文件读入内存,然后在所有更改完成后写出修改后的文件在内存中复制。 – jxh

0

的“不工作”的问题将有更详细受益于如何颂歌它不能正常工作。这是我的最佳镜头。

改变 “%C” 到 “%C”

的OP代码混合scanf("%s",...scanf("%c",...。如果在scanf("%c",...之前,在未发布的代码的某处,您执行了scanf("%s",...或之类的操作,则这是个问题。

scanf("%s",buf)消耗所有前导空格,然后把下面的非白色文本buf留下“回车”(\n)输入缓冲区。接下来的scanf("%c",...会读取(\n),甚至不会等待您输入类似y之类的内容。通过将“%c”更改为“%c”,那么(\n)和额外的空白将会消耗(并扔掉),然后然后将被扫描。

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

+0

感谢您的好评,我不知道。可悲的是还有更多问题需要解决。 “”匹配“\ n”还是我需要“\ n”给他们? – user2647684

+0

在'scanf()'中,''扫描0个或多个空格。 '\ n'扫描0个或多个空格。白色空间扫描0个或更多空格。要显式扫描'\ n',请使用'%[\ n]'。如果您不想将结果保存到char *,请使用'%* [\ n]'。 – chux

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); 
} 
+0

这就是挑战,只需要fscanf fprintf – user2647684

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); 
+0

感谢您指出错误。他们确实让我更接近。 – user2647684