strtok_s()函数在c和c++下的编译问题

北言栾生 2020-10-28 10:57:49
如下这段代码,放在一个.c文件中,Windows下用gcc编译可以通过,但放在一个.cpp文件中Windows下用g++编译报错:error: 'strtok_s' was not declared in this scope
#include <string.h>
#include <stdio.h>

char string[] =".A string\tof ,,tokens\nand some more tokens";
char seps[] = " .,\t\n";
char* token = NULL;
char* next_token = NULL;

int main(void)
{
printf("Tokens:\n");

// Establish string and get the first token:
token = strtok_s(string, seps, &next_token);

// While there are tokens in "string1" or "string2"
while (token != NULL)
{
// Get next token:
if (token != NULL)
{
printf(" %s\n", token);
token = strtok_s(NULL, seps, &next_token);
}
}
printf("the rest token1:\n");
printf("%d", token);
}

请问这是为什么?
...全文
1018 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2020-10-29
  • 打赏
  • 举报
回复
用strtok_r试试,这个在c和c++中应该时都能用的~
赵4老师 2020-10-29
  • 打赏
  • 举报
回复
char *strtok_s(char *restrict str, rsize_t *restrict strmax, const char *restrict delim, char **restrict ptr); (2) (since C11)

65,202

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧