[英]Not getting tokenized array from strtok_s()
I am trying to get an array tokenized with the strtok_s() function, BUT I also want to include the delimiter where the array was tokenized.我正在尝试使用 strtok_s() function 对数组进行标记化,但我还想包含对数组进行标记化的分隔符。 If it is a slash " /" I want the array to have slash wherever there was a token made using the slash "/" character.如果它是斜杠“/”,我希望数组在任何使用斜杠“/”字符制作的标记处都有斜杠。
I have written a function, that takes in the string and delimiter and returns another string with the tokens and delimiter.我写了一个 function,它接受字符串和分隔符并返回另一个带有标记和分隔符的字符串。
However, it does not work as expected and I have no idea why.但是,它没有按预期工作,我不知道为什么。 Also, I am not really good with the C language.另外,我不太擅长 C 语言。
Any help or guidance in the right direction would be appreciated.任何正确方向的帮助或指导将不胜感激。
Below is my main cpp file and the console output下面是我的主要 cpp 文件和控制台 output
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct token_stat {
int n; // for total items in array
char* r_array; // pointer to the array returned from function
};
// function for passing a string and a delimeter and getting back
// the array with tokenized items including the delimeter
token_stat append_token_delim(char* string_in, char splitter[])
{
token_stat result;
char* token = NULL ; // initialize variables for STRTOK_S func
char* next_token = NULL ;
char* token_accum = (char*)malloc(2*sizeof(string_in)) ; // allcate memory twice that of the string we are tokenizing
char* delim = splitter; // pointer to the delimeter in main function
int i = 0;
token = strtok_s(string_in, delim, &next_token); // first call and getting the token
while (token != NULL)
{
token_accum[i] = token[0]; // add the token to token_accum array
token_accum[i + 1] = *delim; // now add the delimeter character next to the 1st token
printf("%s\t%s\n", token, delim); // print token and the delimeter
token = strtok_s(NULL, delim, &next_token); // make call again to strtok to get next token
i = i + 2; // increment index by 2 to get to the next token
}
int numberOfTokens = sizeof(*token_accum) / sizeof(token_accum[0]); // get total items in token_accum array for printing in main
result.n = numberOfTokens; // passing values to TOKEN_STAT STRUCT
result.r_array = token_accum; // passing the array to STRUCT
return result; // returning the struct back
}
// printing the array
void print_tokens(token_stat in_array)
{ printf("Number of Tokens in Array; %d", in_array.n);
}
int main()
{
char str[] = "- Thi;s,ansample()str;ing.";
token_stat main_accum;
char delimeter = '/';
main_accum = append_token_delim(str, &delimeter);
print_tokens(main_accum);
return 0;
}
you want to store multiple strings in the token_accum but you are storing only the first character token_accum[i] = token[0];
您想在 token_accum 中存储多个字符串,但您只存储第一个字符token_accum[i] = token[0];
to store multiple strings you need an array of pointer and you can store the strings like this要存储多个字符串,您需要一个指针数组,您可以像这样存储字符串
token_accum[i] = token;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.