我必须用 C 语言编写一个 shell,并且需要在其中处理 globing,并且只允许我使用函数 glob。但是当我尝试使用它时,它只返回一个结果。
#include <glob.h>
#include <stdlib.h>
int main(int ac, char **av)
{
glob_t s;
unsigned long i = -1;
if (glob(av[1], 0, NULL, &s))
printf("ERROR !\n");
else
while (++i < s.gl_pathc)
printf("%s\n", s.gl_pathv[i]);
return (0);
}
我在一个文件夹中运行此代码,其中有两个 C 文件:replace_glob.c 和 test.c
当我运行这段代码时:
$ ./a.out *.c
replace_glob.c
$
我不明白为什么,我非常感谢您的帮助
最佳答案
在命令行中
./a.out *.c
shell 扩展了 glob 模式,因此您的程序可以看到
{"./a.out", "replace_glob.c", "test.c", NULL}
作为其argv
。您需要引用该模式才能让程序看到它:
./a.out '*.c'
关于c - C 中的 Glob 函数仅给出一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23516556/