发布网友 发布时间:2022-05-06 16:47
共1个回答
热心网友 时间:2023-10-12 13:11
可以用open返回值判断,也可以用readdir扫描追问关键问题是linux下对应的类似API函数是哪个? 想遍历一个目录中的文件 找出对应的文件追答就是opendir、readdir
#include
#include
#include
void print_dir (char *root)
{
DIR *dir;
struct dirent *ptr;
dir = opendir (root);
if (dir == NULL) {
perror ("fail to open dir");
exit (1);
}
errno = 0;
while ( (ptr = readdir (dir)) != NULL) {
if (strcmp (ptr->d_name, ".") == 0 || strcmp (ptr->d_name, "..") == 0)
continue;
if (ptr->d_type == DT_DIR) {
print_dir (path);
}
printf ("%s\n", ptr->d_name);
}
if (errno != 0) {
perror ("fail to read dir");
exit (1);
}
closedir (dir);
}
热心网友 时间:2023-10-12 13:11
可以用open返回值判断,也可以用readdir扫描追问关键问题是linux下对应的类似API函数是哪个? 想遍历一个目录中的文件 找出对应的文件追答就是opendir、readdir
#include
#include
#include
void print_dir (char *root)
{
DIR *dir;
struct dirent *ptr;
dir = opendir (root);
if (dir == NULL) {
perror ("fail to open dir");
exit (1);
}
errno = 0;
while ( (ptr = readdir (dir)) != NULL) {
if (strcmp (ptr->d_name, ".") == 0 || strcmp (ptr->d_name, "..") == 0)
continue;
if (ptr->d_type == DT_DIR) {
print_dir (path);
}
printf ("%s\n", ptr->d_name);
}
if (errno != 0) {
perror ("fail to read dir");
exit (1);
}
closedir (dir);
}