strsep() 的用法及其替代方案
#include <stdio.h>
#include <string.h>
int main() {
char *slogan = "together{kaliya} [namak]";
char *slow_gun = strdup(slogan);
char *token = strsep(&slow_gun, "{");
printf ("\n slow_gun: %s\n token: %s\n", slow_gun, token);
return 0;
}
当我执行它时:
$ cc -o try try_strsep.c
$ ./try
slow_gun: kaliya} [namak]
token: together
但是当我将 char *slogan 更改为:
char *slogan = "kalia} [namak]";
并执行相同的程序时:
$ vi try_strsep.c
$ cc -o try try_strsep.c
$ ./try
slow_gun: (null)
token: kalia} [namak]
我的问题是,所以当我使用 strsep() 并且输入字符串没有我的模式时查找,strsep()的返回错误。我验证 strsep() 是否找不到模式的唯一方法是检查 if (slow_gun == NUll)
。
如果我有 char *slogan = "together{"
那么 strsep
将成功返回 token
但将 slow_gun
返回为空白(不是 null
)
$ cc -o try try_strsep.c
$ ./try
slow_gun:
token: together
有没有办法可以避免这个 IF 检查并依赖函数返回 substr,如果它不存在,则返回 NULL
?
#include <stdio.h>
#include <string.h>
int main() {
char *slogan = "together{kaliya} [namak]";
char *slow_gun = strdup(slogan);
char *token = strsep(&slow_gun, "{");
printf ("\n slow_gun: %s\n token: %s\n", slow_gun, token);
return 0;
}
when I execute it:
$ cc -o try try_strsep.c
$ ./try
slow_gun: kaliya} [namak]
token: together
But when, I change the char *slogan to:
char *slogan = "kalia} [namak]";
and execute the same program:
$ vi try_strsep.c
$ cc -o try try_strsep.c
$ ./try
slow_gun: (null)
token: kalia} [namak]
My Question is, so when I use strsep() and input string does not have the pattern I am looking for, the return of strsep() is wrong. The only way I can validate whether strsep() could not find the pattern is to check if (slow_gun == NUll)
.
If I have char *slogan = "together{"
then strsep
would successfully return token
but returns slow_gun
to blank (not null
)
$ cc -o try try_strsep.c
$ ./try
slow_gun:
token: together
Is there a way I could avoid this IF check and rely on the function to return me the substr and if its not there, return NULL
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,没有办法避免检查
slow_gun == NULL
。 以下是strsep
行为的描述:因此,如果未找到匹配项,
strsep
将返回指向原始字符串的指针,并将slow_gun
输入设置为 NULL。如果分隔符是字符串中的最后一个字符,则该字符将被 '\0' 覆盖,并且
slow_gun
设置为后面的字符,该字符恰好是终止原始字符串的 '\0'。这就是 print 语句打印空字符串的原因。注意您错误地使用了
strdup
,调用者负责在该函数返回的指针上调用free
。No, there's no way to avoid the check
slow_gun == NULL
. Here's a description ofstrsep
's behavior:So, if no match is found
strsep
returns a pointer to the original string and sets theslow_gun
input to NULL.If the delimiter is the last character in the string, that character is overwritten by '\0' and
slow_gun
is set to the following character, which happens to be the '\0' terminating the original string. This is why print statement prints an empty string.NOTE You're using
strdup
incorrectly, the caller is responsible for callingfree
on the pointer returned by that function.这是不对的。
strsep()
返回它找到的第一个标记 - 根据定义,字符串的开头是第一个标记。只是在这种情况下没有找到分隔符来终止标记(因此字符串的其余部分是标记)。strsep()
并不是用于“查找模式” - 它用于根据一组分隔符分隔标记。如果要查找字符,请使用strchr()
或strpbrk()
。That's not right.
strsep()
returns the first token it finds - the beginning of the string is by definition the first token. It's just that no delimiter has been found to terminate the token in this case (so the remainder of the string is the token).strsep()
is not intended to be used to 'find a pattern' - it's used to separate tokens based on a set of delimiters. If you want to find a character, usestrchr()
orstrpbrk()
.strsep 的行为正确 - 从手册页:
第二种情况是正确的 - 由于未找到分隔符,因此第一个参数设置为指向
NULL
并返回原始字符串。正如您所说,您需要检查if (slow_gun == NUll)
来检测这一点。(顺便说一句,这是一个非常令人困惑的变量名称选择)。
strsep is behaving correctly - from the man page:
The second case is correct - since the delimeter isn't found the first parameter is set to point at
NULL
and the original string is returned. As you say, you need to check forif (slow_gun == NUll)
to detect this.(incidentally, that's a horribly confusing choice of variable names).