C基于分隔符拆分字符数组,但使用连续分隔符时失败
我正在尝试使用strtok在C中拆分一个字符数组。我现在已经知道了这一点,但我现在意识到,当有两个连续的分隔符时,who的概念会被抵消 我正在将char数组解析为一个基于索引的结构(我无法发布准确的代码,因为它是用于赋值的,但我将发布类似的代码,并更改赋值细节),例如C基于分隔符拆分字符数组,但使用连续分隔符时失败,c,C,我正在尝试使用strtok在C中拆分一个字符数组。我现在已经知道了这一点,但我现在意识到,当有两个连续的分隔符时,who的概念会被抵消 我正在将char数组解析为一个基于索引的结构(我无法发布准确的代码,因为它是用于赋值的,但我将发布类似的代码,并更改赋值细节),例如 struct test_struct{ int index_1; int index_2; int index_3; int index_4; int index_5; }te
struct test_struct{
int index_1;
int index_2;
int index_3;
int index_4;
int index_5;
}test_struct;
我使用计数器填充此信息,因此每次到达分隔符时,增加此计数器并将数据分配给此索引,例如:
char c_array[50] = "hello,this,is,an,example"
counter = 0;
token = strtok (c_array,",");
while (token != NULL) {
switch(counter){
case 0:
test_struct.index_1 = token;
break;
case 1:
test_struct.index_2 = token;
break;
//repeat this step for the other indexes
}
counter++;
token = strtok (NULL, ",");
}
我知道在这种情况下,案例切换可能是一个糟糕的设计选择,但除此之外,有人能帮我找到解决此问题的方法吗:
问题是,当一个字符数组(基本上是C字符串)包含连续的分隔符时,标记“跳过”这个索引,从而使所有内容都偏离了直线。就拿上面的例子来说
如果字符数组的格式正确,那么当案例5出现时,它将表示第5个“spit string”,因此对于上面的示例,当计数器==5 test_struct.index_5时,将具有值“example”。
现在,如果给定上述代码,如果c_数组[50]=“hello,this,,an,example”
,那么问题是,在数组中现在缺少数据之后,这会扰乱索引,它将“跳过”下一个索引,因为,
之间没有任何“字符串”,因此我得到以下结果,而不是预期的行为:
test_struct.index_1 = "hello"
test_struct.index_2 = "this"
test_struct.index_3 = "an"
test_struct.index_4 = "example"
test_struct.index_5 = "example"
那么,是否有一种方法可以说明是否存在”
,然后将令牌设置为默认值,例如“缺少数据”,这样,在将数据读入正确的索引后,至少我可以单独处理
我希望你明白我的意思
干杯,
克里斯。工作代码
注意:这段代码仍然会修改输入字符串,但可以很好地识别空标记
#include <stdio.h>
#include <string.h>
static void split(char *string)
{
enum { MAX_STRINGS = 5 };
struct test_struct
{
char *index[MAX_STRINGS];
} test_struct;
printf("Splitting: [%s]\n", string);
int i = 0;
char *bgn = string;
char *end;
while (i < MAX_STRINGS && (end = strpbrk(bgn, ",")) != 0)
{
test_struct.index[i++] = bgn;
*end = '\0';
bgn = end + 1;
}
if (i >= MAX_STRINGS)
fprintf(stderr, "Too many strings!\n");
else
test_struct.index[i++] = bgn;
for (int j = 0; j < i; j++)
printf("index[%d] = [%s]\n", j, test_struct.index[j]);
}
int main(void)
{
char c_array[][30] =
{
"hello,this,is,an,example",
"hello,this,,an,example",
"hello,,bad,,example,input",
"hello,world",
",,,,",
",,",
"",
};
enum { C_SIZE = sizeof(c_array) / sizeof(c_array[0]) };
for (int i = 0; i < C_SIZE; i++)
split(c_array[i]);
return 0;
}
你说:我正试图使用
strtok
在C中拆分一个字符数组。这就是你的问题开始的地方。看看你的行为规范,然后后悔你的行为。如果您关心空令牌,那么它不是正确的工具。另外,使用名称index_1
…index_5
不是在向您尖叫“array”(就像在array中一样!),甚至是“array!)吗?应该的!这些变量名称只是我代码中的抽象示例,当我在中复制我的代码时,我更改了它们,因为这是一个赋值,所以我不需要;不要因为抄袭而惹麻烦。你能推荐我应该研究哪些工具,以便以这种方式处理空代币吗;C.查找函数。你可以使用后两者中的一个,最简单的是,与……哦,这很有趣;我刚刚发现您正在尝试将指针分配给整数,这也不是一个好主意。好啊你可以使用strcspn()
或strpbrk()
来找到下一个分隔符,然后安排做任何适当的事情。我明天会看看这些,我来自95%的Java背景,所以我在那里只使用String.split(regex),这让我思考实际发生了什么以及如何解决这些问题,谢谢你的帮助。如果我难以使用这些,你很快就会再次使用我!使用
Splitting: [hello,this,is,an,example]
index[0] = [hello]
index[1] = [this]
index[2] = [is]
index[3] = [an]
index[4] = [example]
Splitting: [hello,this,,an,example]
index[0] = [hello]
index[1] = [this]
index[2] = []
index[3] = [an]
index[4] = [example]
Splitting: [hello,,bad,,example,input]
Too many strings!
index[0] = [hello]
index[1] = []
index[2] = [bad]
index[3] = []
index[4] = [example]
Splitting: [hello,world]
index[0] = [hello]
index[1] = [world]
Splitting: [,,,,]
index[0] = []
index[1] = []
index[2] = []
index[3] = []
index[4] = []
Splitting: [,,]
index[0] = []
index[1] = []
index[2] = []
Splitting: []
index[0] = []