【发布时间】:2021-10-29 12:39:24
【问题描述】:
FILE* f;
fopen("vaccine.txt", "w"); //Open and write on the vaccine.txt file
for (int i = 0; i < 5; i++) { //Allow up to 5 vaccine's recorded at once!
printf("\nName of vaccine %d: ", i + 1);
scanf("%s", vacname, sizeof(vacname), f);
printf("\tVaccine Code: ");
scanf("%s",code, sizeof(code), f);
printf("\tProducing Country: ");
scanf("%s", country, sizeof(country), f);
printf("\tDosage: ");
scanf("%d", &dosage, f);
printf("\tPopulation: ");
scanf("%f", &population, f);
printf("\tQuantity: ");
scanf("%d", &quantity, f);
fprintf(f, "%s\n%s\n%s\n%d\n%.2f\n%d\n", vacname, code, country, dosage, population, quantity);
}
fclose(f);
printf("\nVaccine data successfully recorded!\n\n"); //Notify user of the program successful ru
n
};
【问题讨论】:
-
见this,它解释了
scanf和fscanf之间的区别,以及如何使用它们。 -
你的意思是
scanf("%s", vacname, sizeof(vacname), f);,也许是fscanf(f, "%99s", vacname);?