c - 在 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 上搜索过这个,但任何东西都有助于解决这个问题。有人可以帮助我吗?
解决方案
推荐阅读
- php - 在 Carbon 解析中没有得到正确的日期
- dynamic-data - 请求自定义路由文件时动态更改数据库
- python - 如何使用“--no-binary”编写 requirements.txt 文件?
- visual-studio-code - VSCode 终端进程命令启动失败
- powershell - Powershell下载并执行exe文件
- angular - 使用服务层 HttpTestingController 进行 Angular 单元测试错误:预计未找到打开的请求 GET
- html - 如何使背景图像填充 div 并做出响应
- c# - 无法初始化模拟类的成员
- css - Laravel DataTables 基于值的颜色单元格?
- angular - 面临在 Json 值中为名称查找 id 的问题