【问题标题】:What's the problem here (c program). It shows too many argument s for format. For all the 'scanf' lines这里有什么问题(c程序)。它显示了太多参数的格式。对于所有“scanf”行
【发布时间】: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,它解释了scanffscanf之间的区别,以及如何使用它们。
  • 你的意思是scanf("%s", vacname, sizeof(vacname), f);,也许是fscanf(f, "%99s", vacname);

标签: c function


【解决方案1】:

您不必使用 scanf 指定大小。您可以使用 fgets;它是缓冲安全的

scanf("%s",variable);

【讨论】:

    猜你喜欢
    • 2013-06-03
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多