strtok is不按预期返回,我在使用错误吗?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// fiter string to the first |
char* filterstringfirst(char* command, int i){
char *tok = command;
int x = 0;
while ((tok = strtok(tok, "|")) != NULL && x <= i)
{
if( x == i){
return tok;
}
x++;
printf(" === Parsed: --%s-- ===\n", tok);
tok = NULL;
}
return tok;
}
int main () {
char command[] = "ls -a | sort -h | grep h | wc -l";
char command2[] = "ls -a | sort -h | grep h | wc -l";
char* temp = command;
char* x = filterstringfirst(temp, 0);
printf("%s\n",x);
char* temp2 = command;
char* x2 = filterstringfirst(temp2, 1);
printf("%s\n",x2);
temp = command;
return 0;
}
我拥有的功能应该只是返回字符串的一部分。原始字符串应类似于“ LS -L | Grep Temp | Sort”。
这个想法是将其用字符串和一个数字调用,然后返回该细分市场。例如。 0-&GT; 现在, “ LS -L”
这是我第一次称呼它的时候起作用,但是再次打电话给它似乎在segfault中打破并结束。
char command[] = "ls -a | sort -h | grep h | wc -l";
char* temp = command;
char* x = filterstringfirst(temp, 0);
printf("%s\n",x);
char* temp2 = command;
char* x2 = filterstringfirst(temp2, 1);
printf("%s\n",x2);`
这是我的测试代码
和输出:
ls -a
=== Parsed: --ls -a -- ===
[1] 1126 segmentation fault ./templ
➜ Current gcc -o templ templ.c
➜ Current ./templ ls -a
=== Parsed: --ls -a -- === [1]
1136 segmentation fault ./templ
编辑:更新以具有主(根据注释)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// fiter string to the first |
char* filterstringfirst(char* command, int i){
char *tok = command;
int x = 0;
while ((tok = strtok(tok, "|")) != NULL && x <= i)
{
if( x == i){
return tok;
}
x++;
printf(" === Parsed: --%s-- ===\n", tok);
tok = NULL;
}
return tok;
}
int main () {
char command[] = "ls -a | sort -h | grep h | wc -l";
char command2[] = "ls -a | sort -h | grep h | wc -l";
char* temp = command;
char* x = filterstringfirst(temp, 0);
printf("%s\n",x);
char* temp2 = command;
char* x2 = filterstringfirst(temp2, 1);
printf("%s\n",x2);
temp = command;
return 0;
}
I have this function I made which is supposed to just return part of a string. The original string should be similar to "ls -l | grep temp | sort".
The idea was that it would be called with the string and a number, and return that segment. Eg. 0 -> "ls -l"
Now this works the first time I call it, but calling it again seems to break and end in a segfault.
char command[] = "ls -a | sort -h | grep h | wc -l";
char* temp = command;
char* x = filterstringfirst(temp, 0);
printf("%s\n",x);
char* temp2 = command;
char* x2 = filterstringfirst(temp2, 1);
printf("%s\n",x2);`
This was my testing code
And the output:
ls -a
=== Parsed: --ls -a -- ===
[1] 1126 segmentation fault ./templ
➜ Current gcc -o templ templ.c
➜ Current ./templ ls -a
=== Parsed: --ls -a -- === [1]
1136 segmentation fault ./templ
Edit: Updated to have main too (based on comments)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
strtok
IS 破坏性 - 它通过替换定界符将无效字节来修改传递的缓冲区。命令
将有效地为
“ LS -A”
。如果您想在此处使用
strtok
,则需要:模拟
strtok
在包装函数中,通过传递null
和从随后的电话或开始的位置
null
以及从 令牌。第二个示例,没有错误处理:
stdout
:(请注意这些令牌中的空格。)
strtok
is destructive - it modifies the buffer passed in by replacing delimiters will null bytes.After
command
will effectively be"ls -a "
.If you want to use
strtok
here, you will either need to:mimic
strtok
in your wrapping function, by passingNULL
and the position to start from in subsequent calls, orduplicate the string before using it, and return a copy of the token.
An example of the second, with no error handling:
stdout
:(Note the whitespace in these tokens.)
函数
strtok
通过在分界符的位置插入零字符'\ 0'来更改传递的字符串。因此,在函数的第一个调用
filterstringfirst
字符阵列
命令
看来实际上您具有以下字符串
“ ls -a”
存储在数组命令
中。因此,第二次调用第二个参数大于0的函数,因此您将获得无效指针。
如果要提取指定索引的子字符串,则应使用函数
strspn
和strcspn
,然后从函数返回一个动态分配的包含目标基因的数组。这是一个演示程序,该程序显示如何使用标准字符串函数
strspn
和strcspn
and 而无需动态创建源字符串的副本(该功能每次调用函数时效率低下且不安全)。程序输出是
您可以将此功能与用于分离字符串的任何定界器一起使用。
The function
strtok
changes the passed string by inserting zero characters '\0' in the positions of delimiters.So after the first call of the function
filterstringfirst
the character array
command
looks likeThat is in fact you have the following string
"ls -a "
stored in the arraycommand
.So calling the function the second time with the second argument greater than 0 you will get as a result a null pointer.
If you want to extract substrings specifying an index then you should use functions
strspn
andstrcspn
and return from the function a dynamically allocated array containing the target substring.Here is a demonstration program that shows how the function can be defined using the standard string functions
strspn
andstrcspn
and without creating dynamically a copy of the source string (that is inefficient and unsafe) each time when the function is called.The program output is
You can use this function with any delimiters used to separate a string.