从 2 个文件中减去数量并在其中一个文件中更新

问题描述

如果我想从文件 1 中减去数量文件 2 中的数量,我该怎么做?并更新数量文件 1 中的结果。以下是我目前所做的。

void update(){
vaccine v1;
FILE *fp,*fp1;
int found = 0;
char code[50];
fp = fopen("vaccine.txt","r");
fp1 = fopen("dist.txt","w");
printf ("enter vaccine code to update: ");
scanf ("%s",&code);

while (fread(&v1,sizeof(vaccine),1,fp)) {
    if (strcmp(code,v1.code) == 0) {
        found = 1;
        fflush(stdin);
        printf("Enter distributed quantity:");
        scanf("%i,&v1.quantity");
        break;
    }
    fwrite(&v1,fp1);
}
fclose(fp);
fclose(fp1);
if(found) {
    fp1 = fopen ("dist.txt","r");
    fp = fopen("vaccine.txt","w");
    
    while(fread(&v1,fp)){
        v1.quantity - (while(fread()))
        
    }
}
    printf ("\n record not found");

}

这就是我开始的方式、结构以及我创建文件vaccine.txt的时间

typedef struct vaccine {
char name [50];
char origin [50];
char code [50]; 
int dosage;
char population_covered [50];
int quantity;   

}疫苗;

这将是创建函数

void create (){
vaccine *v;
FILE *fp; 
int amount,i;
printf("enter amount you wished to edit: ");
scanf("%i",&amount);

v = (vaccine*)calloc(amount,sizeof(vaccine));
fp = fopen("vaccine.txt","w");

for(i=0;i<amount;i++){
    fflush(stdin);
    printf("enter vaccine's name: \n");
    scanf("%[^\n]s",v[i].name);
    fflush(stdin);
    printf("enter vaccine's code: \n");
    scanf("%[^\n]s]",v[i].code);
    fflush(stdin);
    printf("enter vaccine's production country: \n");
    scanf("%[^\n]s",v[i].origin);
    fflush(stdin);
    printf("enter vaccine's dosage: \n");
    scanf("%i",&v[i].dosage);
    fflush(stdin);
    printf("enter vaccine's population covered: \n");
    scanf("%[^\n]s",v[i].population_covered);
    fflush(stdin);
    printf("enter vaccine's quantity: ");
    scanf("%i",&v[i].quantity);
    fwrite(&v[i],fp);
} 
fclose(fp);

}

这两个只是函数,我已经在主代码调用

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)