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 以外は受け付けません。

助けてくれてありがとう。

編集: 完全なコードが更新され、割り当てが変更され、単一のファイルを置き換える必要があります。2 番目のファイルを使用することはできません。

4

4 に答える 4

0

これはうまくいくかもしれません。あなたのコードに関する2つの間違いを修正しました。

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",...、未公開コードのどこかで a などを実行した場合に問題になりますscanf("%s",...

scanf("%s",buf)、先頭の空白をすべて消費してから、次の非白のテキストを に入れ、「入力」( ) を入力バッファbuf残します。\nscanf("%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 に答える