gpt4 book ai didi

将 gchar 与字符串进行比较

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:57 25 4
gpt4 key购买 nike

我使用以下代码比较 gchar 中的字符串存储与从文件中读取的 char 数组:

while (fgets(line, sizeof line, config) != NULL)
{
printf("line content is %s",line+5);
printf("size of comparison: %i\n",g_utf8_strlen(active_selection,-1));
if (g_ascii_strncasecmp(line+5,active_selection,g_utf8_strlen(active_selection,-1)+1)==1)
{
printf("success \n\n");
}
else
printf("failure\n");
}

配置文件的值是:

set "hello"
button "hello" "some text"
button "phone" "more text"
set "pmrs"
button "test" "test even more text"

printf() 显示以下值:

line+5 = hello"
line+5 (second iteration) = on "hello"
active_selection = hello
g_utf8_strlen(active_selection,-1) = 5

我所期望的是第一次迭代 (hello") 将返回 true第二个将返回 false(在 "hello" 上),认为只会比较前 5 个字符。

显然情况并非如此,因为我总是执行 else 语句(我使用 printf 进行测试)。

最佳答案

所有字符串比较函数在匹配时返回 0。所以你的 if 条件应该检查​​ 0 而不是 1

https://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-ascii-strncasecmp :

Returns

0 if the strings match, a negative value if s1 < s2 , or a positive value if s1 > s2 .

关于将 gchar 与字符串进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32750104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com