2016-12-10 5 views
1

私は、ファイルにのみ、特定の値を編集したい、 ファイル内の特定の値を編集するにはどうすればよいですか?

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main() 
{ 
    int month[3]={0,0,0}; 
    int month1; 
    printf("\n1.December"); 
    printf("\n2.November"); 
    printf("\nEnter month:"); 
    scanf("%d",&month1); 

    if(month1 == 2) 
    { 
     printf("\nthis is November"); 
     month[1]=3*5; 
     printf("\ntotal for this month is :%d",month[1]); 
    } 

     else if (month1 == 1) 
    { 
     printf("\nthis is December"); 
     month[2]=2*5; 
     printf("\ntotal for this month is :%d\n\n\n",month[2]); 

    } 

    FILE *mo; 
    if((mo = fopen ("month.txt", "w")) == NULL) 
     { 
     printf ("File unable to open"); 
     } // end if 
    else 
    { 
     fprintf(mo,"Transaction for December is %d ",month[2]); 
     fprintf(mo,"\nTransaction for November is %d ",month[1]); 

    } 


return 0; 
system("pause"); 
} 

のは、私は一度このプログラムを実行し、その後、プログラムはファイルでこの値を格納するとしましょう。 と12月のどのファイルmonth.txt内部

トランザクションの下に11月10 取引では、私は再びこのプログラムを実行した後、0

作るためにどのように、10の値は自動的に0に関連付けるを変更しませんその値を更新します。 そして、それは(私は3時間このプログラムを実行した後) は、最初のiは、1秒を選択し、第三、私は2を選択し、ファイルmonth.txt でこのように見えるように、12月の

取引は11月の10 取引では30

です

答えて

0

これを行う方法はありません。あなたのファイルは書式なしのテキストであることを暗示しているので、「12月の取引」と「11月の取引」というフレーズはデリミタやタグのように扱う必要があります。

解決策の1つの例は、ドキュメント全体をスキャンし、次に必要な文字列を置換するコードにwhileループを追加することです。私の提案する実装​​では、fgetsを使用して線を引いてから、strstrで解析します。もちろん、strtokなどの他のさまざまな関数を使用することもできます。

ただし、これは必ずしも安全な操作ではないことに注意してください。代わりにあなたのプログラムをソースファイルから各行をコピーし、完全に別のファイルに出力する方が良いでしょう。しかし、その振る舞いはほぼ同じです。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main() 
{ 
    int month[3] = { 0,0,0 }; 
    int month1; 
    printf("\n1.December"); 
    printf("\n2.November"); 
    printf("\nEnter month:"); 
    scanf("%d", &month1); 

    if (month1 == 2) 
    { 
     printf("\nthis is November"); 
     month[1] = 3 * 5; 
     printf("\ntotal for this month is :%d", month[1]); 
    } 

    else if (month1 == 1) 
    { 
     printf("\nthis is December"); 
     month[2] = 2 * 5; 
     printf("\ntotal for this month is :%d\n\n\n", month[2]); 

    } 

    FILE *mo = fopen("month.txt", "w"); 
    if (mo == NULL) 
    { 
     printf("File unable to open"); 
    } // end if 
    else 
    { 
     // Value to edit 
     char phraseToUpdate[] = "Transaction for December is "; 
     char currentLine[50]; 

     // Scan an initial line 
     fgets(currentLine, 49, mo); 

     // Look at every line of the file 
     while (strchr(currentLine, EOF) == NULL) 
     { 
      // Find your phrase 
      if (strstr(currentLine, phraseToUpdate) != NULL) 
      { 
       // Write to your file 
       // You WILL have to modify these to update on the current line 
       // fprintf(mo, "Transaction for December is %d ", month[2]); 
       // fprintf(mo, "\nTransaction for November is %d ", month[1]); 
      } 
      else 
      { 
       // Put that thing back where it came from (or so help me) 
       fprintf(mo, currentLine); 
      } 

      // Scan next line 
      fgets(currentLine, 49, mo); 
     } 

    } 


    return 0; 
    system("pause"); 
} 
関連する問題