【发布时间】:2017-04-25 23:13:24
【问题描述】:
我只想编辑文件中的某些值,
#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");
}
假设我运行这个程序一次,然后程序将把这个值存储在文件中。 以及下面文件month.txt的内容
12 月的交易是 10 日 11 月的交易为 0
如何使,在我再次运行该程序后,10 的值不会改变与 0 的关联,它会自动更新该值。 并使其在文件month.txt中看起来像这样 (在我运行了三遍这个程序之后) 首先我选择 1 第二和第三我选择 2 ,
12 月的交易是 10 日 11 月的交易是 30
【问题讨论】: