我有一个关于fts(3)的问题。每当我试图访问fts_children()函数的任何成员时,我都会得到一个分段错误。当我阅读http://www.kernel.org/doc/man-pages/online/pages/man3/fts.3.html的手册页时,它声称在read函数运行后填充了它自己,并返回一个通过结构中的link字段链接的链表。我怀疑child_function没有返回任何内容,但我觉得这与手册页不符。我是否应该将这些文件添加到子缓冲区,因为我认为这是自动完成的?我的代码如下,谢谢!
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fts.h>
#include<string.h>
int compare (const FTSENT**, const FTSENT**);
int main(int argc, char* const argv[])
{
FTS* file_system = NULL;
FTSENT* child = NULL;
FTSENT* parent = NULL;
FTSENT* temp = NULL;
file_system = fts_open(argv + 1,FTS_COMFOLLOW | FTS_NOCHDIR,&compare);
while( (parent = fts_read(file_system)) != NULL)
{
child = fts_children(file_system,0);
printf("%s\n", child->fts_path);
}
// while (child ->fts_link != NULL)
// child = child->fts_link;
fts_close(file_system);
return 0;
}
int compare(const FTSENT** one, const FTSENT** two){
return (strcmp((*one)->fts_name, (*two)->fts_name));
}
"test_fs.c" 43L, 1108C 发布于 2012-09-27 04:51:24
您只需添加一个空检查。
你可能想要
file_system 如果成功,fts_children()函数将返回一个指向FTSENT结构的指针,该结构描述目录中以NULL终止的文件链接列表中的第一个条目。fts_children()函数可能会失败,并为chdir()、malloc()、opendir()、readdir()和stat()函数指定的任何错误设置errno。
将更新为评论中的新问题:
当你在做的时候:
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fts.h>
#include<string.h>
#include<errno.h>
int compare (const FTSENT**, const FTSENT**);
int main(int argc, char* const argv[])
{
FTS* file_system = NULL;
FTSENT* child = NULL;
FTSENT* parent = NULL;
if (argc<2)
{
printf("Usage: %s <path-spec>\n", argv[0]);
exit(255);
}
file_system = fts_open(argv + 1,FTS_COMFOLLOW | FTS_NOCHDIR,&compare);
if (NULL != file_system)
{
while( (parent = fts_read(file_system)) != NULL)
{
child = fts_children(file_system,0);
if (errno != 0)
{
perror("fts_children");
}
while ((NULL != child)
&& (NULL != child->fts_link))
{
child = child->fts_link;
printf("%s%s\n", child->fts_path, child->fts_name);
}
}
fts_close(file_system);
}
return 0;
}
int compare(const FTSENT** one, const FTSENT** two)
{
return (strcmp((*one)->fts_name, (*two)->fts_name));
}输出片段示例:
./.profiles/sehe/.opera/icons/cache/g_0000
./.profiles/sehe/.opera/icons/cache/g_0000/opr00002.tmp
./.profiles/sehe/.opera/icons/cache/g_0000/opr00003.tmp
./.profiles/sehe/home/sehe/.mozilla
fts_children: Permission denied
./.vbox-sehe-ipc/lock发布于 2014-03-12 22:31:03
如果只对遍历指定路径的所有目录和文件感兴趣,只需重复调用fts_read即可。
如果你只想遍历这个流,@sehe的例子可以重写为:
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<fts.h>
#include<string.h>
#include<errno.h>
int compare (const FTSENT**, const FTSENT**);
void indent (int i);
int main(int argc, char* const argv[])
{
FTS* file_system = NULL;
FTSENT *node, = NULL;
if (argc<2)
{
printf("Usage: %s <path-spec>\n", argv[0]);
exit(255);
}
file_system = fts_open(argv + 1,FTS_COMFOLLOW|FTS_NOCHDIR,&compare);
if (NULL != file_system)
{
while( (node = fts_read(file_system)) != NULL)
{
switch (node->fts_info)
{
case FTS_D :
case FTS_F :
case FTS_SL:
indent(node->fts_level);
printf("%s\n", node->fts_name);
break;
default:
break;
}
}
fts_close(file_system);
}
return 0;
}
int compare(const FTSENT** one, const FTSENT** two)
{
return (strcmp((*one)->fts_name, (*two)->fts_name));
}
void indent(int i)
{
for (; i > 0; i--)
printf(" ");
}当您运行它时,它会遍历流,并按顺序列出所有文件和目录:
★ mkdir -p test/A/1 test/A/2 test/B/1 test/B/2
★ tree test
test
├── A
│ ├── 1
│ └── 2
└── B
├── 1
└── 2
★ ./fts test
test
A
1
2
B
1
2仅当需要特定目录的子节点列表时,才调用fts_children。在这种情况下,必须在调用fts_children之前至少调用fts_read一次;否则fts_children将只向fts_open返回argv参数中指定的节点。
https://stackoverflow.com/questions/12609747
复制相似问题