将 gchar 与字符串进行比较
comparing gchar to a string
我正在使用以下代码将 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"
) 会 return true
第二个会 return false
(在 "hello"
上),
认为只会比较前 5 个字符。
显然情况并非如此,因为我总是执行 else
语句(我使用 printf
进行测试)。
所有字符串比较函数 return 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
中的字符串存储与从文件中读取的 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"
) 会 return true
第二个会 return false
(在 "hello"
上),
认为只会比较前 5 个字符。
显然情况并非如此,因为我总是执行 else
语句(我使用 printf
进行测试)。
所有字符串比较函数 return 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 .