首页 > 解决方案 > 在 strcpy() 中使用 strtok() 时出现未知错误

问题描述

我正在尝试使用从输入中删除换行符strtok()并将其传递给结构属性使用strcpy(),但是当我执行此操作时,Visual Studio 代码返回此消息:

在此处输入图像描述

我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <locale.h>

typedef struct
{
    char name[50];
    char document[50];
    char cep[50];
    char phone[50];
    char address[50];
    char birthdate[50];
    char email[50];
    char diagnosticDate[50];
    int age;
    char comorbidities[50];
} Ocurrence;

void insert_new_record()
{
    Ocurrence new_record;
    char comorbity_option[3];
    printf("Choose option number: ");
    fgets(&comorbity_option, sizeof(comorbity_option), stdin);
    switch (comorbity_option[0])
    {
    case '1':
        strcpy(new_record.comorbidities, "Diabetes");
        break;
    case '2':
        strcpy(new_record.comorbidities, "Obesidade");
        break;
    case '3':
        strcpy(new_record.comorbidities, "Hipertensão");
        break;
    case '4':
        strcpy(new_record.comorbidities, "Tuberculose");
        break;
    case '5':
        strcpy(new_record.comorbidities, "Outros");
        break;
    default:
        strcpy(new_record.comorbidities, "Nenhuma");
        break;
    }

    strcpy(new_record.name, strtok(new_record.name, "\n"));
    strcpy(new_record.cep, strtok(new_record.cep, "\n"));
    strcpy(new_record.address, strtok(new_record.address, "\n"));
    strcpy(new_record.phone, strtok(new_record.phone, "\n"));
    strcpy(new_record.birthdate, strtok(new_record.birthdate, "\n"));
    strcpy(new_record.diagnosticDate, strtok(new_record.diagnosticDate, "\n"));
    strcpy(new_record.document, strtok(new_record.document, "\n"));
    strcpy(new_record.email, strtok(new_record.email, "\n"));
}

int main()
{
    setlocale(LC_ALL, "Portuguese");
    show_login();
    show_menu();
    return 0;
}

在调试模式下可以验证错误是否显示在行后:strcpy(new_record.name, strtok(new_record.name, "\n"));

我在 StackOverflow 上搜索过这个,但任何东西都有助于解决这个问题。有人可以帮助我吗?

标签: cpointersvisual-studio-codestrtokstrcpy

解决方案



推荐阅读