167 字
1 分钟
Usage of the strtok_s() Function

When you need performance optimization, consider C printf and sprintf, which are fast and easy to use. However, they are not extensible or immune to attacks. (Security versions exist, but they incur a slight performance penalty. For details, see printf_s, _printf_s_l, wprintf_s, _wprintf_s_l and sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l. By MSDN, some old functions, including strtok(), seem to have some security issues and are replaced by strtok_s(). The new function adds one more parameter, and it feels more usable than the old one. Usage example below

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
char c_string[] =
"1 2 3 4 5";
char sepa[] = " ";
char *token = NULL;
char *next_token = NULL;
int main()
{
// Establish string and get the first token:
token = strtok_s(c_string, sepa, &next_token);
// While there are tokens in "string1" or "string2"
while (token != NULL)
{
// Get next token:
if (token != NULL)
{
printf_s(" %s\n", token);
token = strtok_s(NULL, sepa, &next_token);
}
}
printf("remain:\n");
printf("%d", token);
}
Usage of the strtok_s() Function
https://tski.uk/blog/en/cpp-strtok-s/
作者
Tokisaki Galaxy
发布于
2020-08-28
许可协议
CC BY