【问题标题】:can anyone tell me where is the mistakes? [duplicate]谁能告诉我错误在哪里? [复制]
【发布时间】:2021-09-07 05:00:35
【问题描述】:

当程序运行时,在它的中间(在输入疫苗名称后)它会停止并给我 {Exception Thrown} 谁能帮我解决这个问题?我还附上了它给我的错误的照片

 #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    int main()
    {
        int choose, dosage, quantity;
        char code[10];
        char name[32];
        char produce[64];
        float population;
        {
            printf("COVID-19 Vaccination System \n");
            printf("1:- Inventory Creation \n");
            printf("2:- Update Vaccine Quantities \n");
            printf("3:- Search Vaccine \n");
            printf("4:- Produce a list of all Vaccines and their distributed Quantities \n");
            printf("5:- EXIT \n\n");
            printf("==>");
            scanf_s("\n%d", &choose);
            if (choose == 1)
            {
                char ch;
                FILE* cv;
                errno_t err;
                err = fopen_s(&cv, "vaccine.txt", "w");
                for (int i = 0; i < 5; i++)
                {
                    printf("Please Enter Vaccine Full information %d \n", i + 1);
                    printf(" Name of Vaccine : ");
                    scanf_s("\n%s", &name);
                    printf("Please Enter Vaccine Code %c \n", i + 1);
                    printf(" Vaccine Code : ");
                    scanf_s("\n%s", &code);
                    printf("Please Enter Vaccine Producing Country %c \n", i + 1);
                    printf("Producing Country : ");
                    scanf_s("\n%s", &produce);
                    printf("Please Enter Dosage Required %d \n", i + 1);
                    printf("Dosage Required (MAX=2 - MIN=1) : ");
                    scanf_s("\n%d", &dosage);
                    printf("Please Enter Population Covered %d \n", i + 1);
                    printf("Population Covered (%) : ");
                    scanf_s("\n%f", &population);
                    printf("Please Enter Vaccine Quantity %d \n", i + 1);
                    scanf_s("\n%d", &quantity);
    
                    fprintf("%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
                }
            }
            
        }
        return 0;

}

【问题讨论】:

  • docs.microsoft.com/en-us/cpp/c-runtime-library/reference/… [... 指定所有 c、C、s、S 或字符串控制集 [] 参数的大小。 ...]
  • 根据this,无论何时使用%s,scanf_s 都应该有两个参数——一个参数是指向缓冲区的指针,第二个参数是缓冲区的大小。跨度>
  • 是否存在不需要 size 参数的 scanf_s() 重载?如果没有,它是如何构建的?
  • 你为什么用scanf_s而不是scanf?是因为 Microsoft Visual Studio 告诉你这样做的吗?如果是这种情况,那么我建议您禁用此错误消息,方法是在代码开头添加 #define _CRT_SECURE_NO_WARNINGS(在任何 #include 指令之前)。
  • @AndreasWenzel 这是个坏建议。 scanf 可以溢出输入缓冲区。 scanf_s 的存在是有原因的(很遗憾它没有被其他供应商采用)。

标签: c txt


【解决方案1】:

线

fprintf("%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);

错了。您必须将文件指针作为fprintf() 的第一个参数传递。应该是:

fprintf(cv, "%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);

还有

  • 在进行下一个操作之前,您应该检查fopen_s 是否成功,如果失败则停止操作。
  • 您应该删除scanf_s 中使用的数组名称(code、name、produce)之前的&amp;。表达式中的数组(不包括一些例外)会自动转换为指向第一个元素的指针,因此这里不需要显式的&amp;。它还会使类型错误scanf() 期望char* 对应%s,但&amp;code 之类的东西是指向数组的指针(例如char(*)[10])。

【讨论】:

  • 我认为导致未定义行为的原因不是您在问题中描述的内容,而是可变参数 scanf_s 函数调用中 %s 格式说明符的错误参数数量(请参阅cmets 了解更多信息)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
相关资源
最近更新 更多