【发布时间】:2012-09-27 15:38:48
【问题描述】:
对于这个 fts_children() 问题,我一直在敲我的头。在手册页中,http://www.kernel.org/doc/man-pages/online/pages/man3/fts.3.html,它明确指出As a special case, if fts_read() has not yet been called for a hierarchy,
fts_children() will return a pointer to the files in the logical directory
specified to fts_open(), that is, the arguments specified to fts_open().
我的意思是返回当前目录中所有文件的链接列表。好吧,我发现情况并非如此,我非常感谢在这件事上提供一些帮助。我希望返回一个链接列表,然后我会遍历它以找到具有匹配文件名的文件(最终目标)。但是,现在,我只是想遍历链表(小步骤)。现在,它将返回一个文件,然后退出循环。这对我来说没有意义。任何帮助将不胜感激!!!
打开文件系统:
char* const path[PATH_MAX] = {directory_name(argv[argc-index]), NULL};
char* name = file_name(argv[argc-index]);
if ((file_system = fts_open(path, FTS_COMFOLLOW, NULL)) == NULL){
fprintf(stderr,"%s:%s\n", strerror(errno), getprogname());
exit(EXIT_FAILURE);
}/*Ends the files system check if statement*/
/*Displays the information about the specified file.*/
file_ls(file_system,name, flags);
为澄清起见,directory_name 解析用户输入的路径并返回类似 /home/tpar44 的内容。然后打开该目录。
在文件系统中搜索:
void
file_ls(FTS* file_system, char* file_name, int* flags){
FTSENT* parent = NULL;
//dint stop = 0;
parent = fts_children(file_system, 0);
while( parent != NULL ){
printf("parent = %s\n", parent->fts_name);
parent = parent->fts_link;
}
}
谢谢!
【问题讨论】:
-
char* const path[PATH_MAX] = {directory_name(argv[argc-index]), NULL};- 这是一个 PATH_MAX c 样式字符串的数组,但只填充了 2 个条目,第二个 NULL? -
@sehe 它实际上是一个二维数组,不是吗?我必须将 fts_open 的文件系统变量设为 char* const* 所以我就是这样做的。我知道它很糟糕,但我不知道如何使用它。此外,第二个参数为 NULL 以防止迭代结束。
-
还不错,但使用 PATH_MAX 显示混乱。更好的是,您可以将其省略,因为初始化程序将允许编译器推断数组大小。 (除其他事项外,我已经发布了一个答案)
标签: c linux filesystems system-calls traversal