GLib库给了我无限的char**篇幅。如何遍历它,打印数组中的每个字符串?
我已经尝试了以下代码,但是即使数组包含多个字符串,它也只会给我第一个字符串。
#include <stdio.h>
#include <glib.h>
static gchar** input_files = NULL;
static const GOptionEntry command_entries[] = {
{"input", 'i', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING_ARRAY, &input_files, "Input files", NULL},
{NULL}
};
int main(int argc, char **argv) {
GOptionContext* option_context;
GError* error;
option_context = g_option_context_new(NULL);
g_option_context_add_main_entries(option_context, command_entries, NULL);
if (!g_option_context_parse(option_context, &argc, &argv, &error)) {
g_printerr("%s: %s\n", argv[0], error->message);
return 1;
}
g_option_context_free(option_context);
if (input_files) {
for (int i = 0; input_files[i]; i++) {
printf("%s", input_files[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
$ ./a.out -i One Two Three
One
Run Code Online (Sandbox Code Playgroud)
G_OPTION_ARG_STRING_ARRAY该选项带有一个字符串参数,该选项的多种用法被收集到一个字符串数组中。
(强调我的)。您必须多次使用该选项才能在数组中获取多个字符串。
./a.out -i One -i Two -i Three
Run Code Online (Sandbox Code Playgroud)