输入所有子文件夹 - 递归
Enter all subfolders - Recursive
如何编写一个进入所有文件夹子文件夹的程序?
我编写了一些代码,但它没有输入子文件夹。
void main(int argc, char *argv[])
{
char* dirPath = argv[1];
struct stat statbuf;
DIR *dir;
struct dirent *ent;
size_t arglen = strlen(argv[1]);
if ((dir = opendir (dirPath)) != NULL) {
while ((ent = readdir (dir)) != NULL) {
printf(ent->d_name, "%sn");
}
closedir (dir);
} else {
perror ("Problem");
}
}
我尝试递归使用stat()函数。
http://www.lemoda.net/c/recursive-directory/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
/* "readdir" etc. are defined here. */
#include <dirent.h>
/* limits.h defines "PATH_MAX". */
#include <limits.h>
/* List the files in "dir_name". */
static void
list_dir (const char * dir_name)
{
DIR * d;
/* Open the directory specified by "dir_name". */
d = opendir (dir_name);
/* Check it was opened. */
if (! d) {
fprintf (stderr, "Cannot open directory '%s': %sn",
dir_name, strerror (errno));
exit (EXIT_FAILURE);
}
while (1) {
struct dirent * entry;
const char * d_name;
/* "Readdir" gets subsequent entries from "d". */
entry = readdir (d);
if (! entry) {
/* There are no more entries in this directory, so break
out of the while loop. */
break;
}
d_name = entry->d_name;
/* Print the name of the file and directory. */
printf ("%s/%sn", dir_name, d_name);
#if 0
/* If you don't want to print the directories, use the
following line: */
if (! (entry->d_type & DT_DIR)) {
printf ("%s/%sn", dir_name, d_name);
}
#endif /* 0 */
if (entry->d_type & DT_DIR) {
/* Check that the directory is not "d" or d's parent. */
if (strcmp (d_name, "..") != 0 &&
strcmp (d_name, ".") != 0) {
int path_length;
char path[PATH_MAX];
path_length = snprintf (path, PATH_MAX,
"%s/%s", dir_name, d_name);
printf ("%sn", path);
if (path_length >= PATH_MAX) {
fprintf (stderr, "Path length has got too long.n");
exit (EXIT_FAILURE);
}
/* Recursively call "list_dir" with the new path. */
list_dir (path);
}
}
}
/* After going through all the entries, close the directory. */
if (closedir (d)) {
fprintf (stderr, "Could not close '%s': %sn",
dir_name, strerror (errno));
exit (EXIT_FAILURE);
}
}
int main ()
{
list_dir ("/usr/share/games");
return 0;
}
另一个示例,使用文件树步行(ftw
或nftw
)这些优势是使用struct stat
,文件名和类型(FTW_D等)提供您自己的回调函数,以消除不需要的条目。以"开头的文件。是"隐藏文件"。ls
默认不显示它们。ls -a
将列出它们。
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <ftw.h>
int callback(const char *fname,
const struct stat *st,
int type,
struct FTW *ftw)
{
// call fnmatch() here or use type to decide about printing
// printf file name and type , ??? on stat error FTW_NS, default to "????"
printf("%sn", fname);
return 0;
}
int main(int argc, char **argv)
{
int fd_max=8; // max file desriptors
int retval=nftw( (argc==1)?"." :argv[1], callback, fd_max, FTW_ANYERR);
return retval;
}
相关文章:
- 递归计数给定目录的文件和所有目录
- 包含模板文件的递归会导致编译失败
- 递归地将FEATURE_FLAG导出到生成文件、CPP 和头文件
- 如何递归复制文件和目录
- 使用FindFirstFile和FindNextFile递归查找具有特定扩展名的文件
- boost::文件系统递归获取每个文件的大小
- C++ 源代码的递归生成文件
- 递归功能轨道目录未检测到所有文件夹
- 问题 - 递归函数以返回文本文件排列
- 使用C 列出目录中的文件,而不是递归,只有文件和无子目录
- 递归递归文件
- 使用boost递归地将给定目录中每个最低级别文件夹的文件复制到另一个目录-C++
- 输入所有子文件夹 - 递归
- Qt vs Boost 文件系统递归文件计数
- 在Qt中递归浏览目录,跳过文件夹"."并".."
- 如何使用 Boost 和 C++ 递归和单独列出文件和文件夹
- 递归文件搜索出现问题
- FTP递归文件上传,下载和删除
- 递归文件夹读取c++
- 如何判断文件夹是否是递归文件夹复制的子文件夹