[英]Using fscanf to read text then printing that text into a file using fprintf
[英]Replace a string in text file with just fprintf and 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。
谢谢你的帮助。
编辑:完整代码更新,分配更改,单个文件需要替换,我不允许使用第二个文件。
由于您已经有两个文件,因此请同时打开两个文件。 当您从一行中读取每一行时,您可以将相同的数据写入另一行,或者将新数据写入另一行,具体取决于用户的选择。
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”
OP代码将scanf("%s",...
与scanf("%c",...
混合在一起。如果在scanf("%c",...
之前的未发布代码中,执行了scanf("%s",...
等。
scanf("%s",buf)
会消耗所有前导空白,然后将以下非白色文本放入buf
, 将 “ enter”( \\n
) 保留在输入缓冲区中。 随后的scanf("%c",...
将读取( \\n
),甚至不等您键入y
。通过将“%c”更改为“%c”,该( \\n
)和其他空白将被消耗(并扔掉), 然后您的y
将被扫描。
此外,考虑检查scanf()和fscanf()的返回值。 它将有效地帮助您调试代码。
您需要做的不仅仅是扫描和打印。 这是一些伪代码供您使用:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.