strsep() 的用法及其替代方案

发布于 2024-11-27 00:33:08 字数 1107 浏览 0 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身仙ぐ女味 2024-12-04 00:33:08

不,没有办法避免检查slow_gun == NULL以下是 strsep 行为的描述

char *strsep(char **stringp, const char *delim);

描述
如果*stringpNULL,则strsep()函数返回NULL 并且不执行任何其他操作。否则,此函数将查找字符串 *stringp 中的第一个标记,其中标记由字符串 delim 中的符号分隔。该令牌通过用空字节 ('\0') 覆盖分隔符来终止,并且 *stringp 更新为指向该令牌之后的位置。如果未找到分隔符,则标记将被视为整个字符串 *stringp,并且 *stringp 将被设为 NULL

返回值
strsep()函数返回一个指向token的指针,即返回*stringp的原始值代码>.

因此,如果未找到匹配项,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 of strsep's behavior:

char *strsep(char **stringp, const char *delim);

DESCRIPTION
If *stringp is NULL, the strsep() function returns NULL and does nothing else. Otherwise, this function finds the first token in the string *stringp, where tokens are delimited by symbols in the string delim. This token is terminated by overwriting the delimiter with a null byte ('\0') and *stringp is updated to point past the token. In case no delimiter was found, the token is taken to be the entire string *stringp, and *stringp is made NULL.

RETURN VALUE
The strsep() function returns a pointer to the token, that is, it returns the original value of *stringp.

So, if no match is found strsep returns a pointer to the original string and sets the slow_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 calling free on the pointer returned by that function.

尽揽少女心 2024-12-04 00:33:08

<块引用>

strsep()返回错误

这是不对的。 strsep() 返回它找到的第一个标记 - 根据定义,字符串的开头是第一个标记。只是在这种情况下没有找到分隔符来终止标记(因此字符串的其余部分是标记)。

strsep() 并不是用于“查找模式” - 它用于根据一组分隔符分隔标记。如果要查找字符,请使用 strchr()strpbrk()

the return of strsep() is wrong

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, use strchr() or strpbrk().

如果没有你 2024-12-04 00:33:08

strsep 的行为正确 - 从手册页

strsep() 函数在 *stringp 引用的字符串中定位,
字符串 delim 中任何字符的第一次出现(或
终止 \0 字符)并将其替换为 \0。的位置
分隔符之后的下一个字符(或 NULL,如果
到达字符串)存储在*stringp中。的原始值
返回*stringp

第二种情况是正确的 - 由于未找到分隔符,因此第一个参数设置为指向 NULL 并返回原始字符串。正如您所说,您需要检查 if (slow_gun == NUll) 来检测这一点。

(顺便说一句,这是一个非常令人困惑的变量名称选择)。

strsep is behaving correctly - from the man page:

The strsep() function locates, in the string referenced by *stringp,
the first occurrence of any character in the string delim (or the
terminating \0 character) and replaces it with a \0. The location of the
next character after the delimiter character (or NULL, if the end of
the string was reached) is stored in *stringp. The original value of
*stringp is returned.

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 for if (slow_gun == NUll) to detect this.

(incidentally, that's a horribly confusing choice of variable names).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文