我在使用wordexp
. 如果这个函数找不到任何文件,它会像找到 1 一样返回。
#include <stdio.h>
#include <wordexp.h>
#include <string.h>
int main(int argc, char* argv[])
{
if (argc < 2)
return -1;
printf("searching for %s:\n", argv[1]);
wordexp_t p;
memset(&p, 0, sizeof p);
if (wordexp(argv[1], &p, 0) != 0)
return -1;
char **w = p.we_wordv;
printf("p.we_offs = %zu\n", p.we_offs);
printf("p.we_wordc = %zu\n", p.we_wordc);
for (unsigned int i = 0; i < p.we_wordc; i++)
{
printf("file found: %s\n", w[i]);
}
wordfree(&p);
return 0;
}
调用这个程序的./a.out "test*.c"
结果是
searching for test*.c:
p.we_offs = 0
p.we_wordc = 2
file found: test1.c
file found: test2.c
但调用它的./a.out "test0*.c"
结果是
searching for test0*.c:
p.we_offs = 0
p.we_wordc = 1
file found: test0*.c
不应该p.we_wordc
等于0
,因为找不到文件?
提前致谢