c/c++遍历文件
一般使用opendir方式
1.使用ftw方法
ftw.h头文件不是标准c库,MSVC是没有该头文件的,需要mingw
int ftw(const char *dirpath, int (*fn)(const char *fpath, const struct stat *sb, int typeflag), int nopenfd);
dirpath: 要遍历的根目录的路径。
fn: 一个用户定义的回调函数,用于处理每个找到的文件和目录。这个函数会被调用多次,每次传递不同的文件或目录信息给它。
nopenfd: 指定同时打开的文件描述符的最大数目,通常可以设置为 0(0表示不限制同时打开的文件描述符的数量,函数将根据需要打开文件)。这个参数主要限制限制资源的使用,在速度和资源情况下设置
int nftw(const char *dirpath, int (*fn)(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf), int nopenfd, int flags);
dirpath: 要遍历的根目录的路径。
fn: 一个用户定义的回调函数,用于处理每个找到的文件和目录。这个函数会被调用多次,每次传递不同的文件或目录信息给它。
nopenfd: 指定同时打开的文件描述符的最大数目,通常可以设置为 0(0表示不限制同时打开的文件描述符的数量,函数将根据需要打开文件)。这个参数主要限制限制资源的使用,在速度和资源情况下设置
flags: 用于控制遍历行为的标志,比如是否要递归遍历子目录、是否要跟踪符号链接等。
结构体struct stat
在#include <sys/stat.h>
struct stat {
_dev_t st_dev; // 文件所在的设备的ID。
_ino_t st_ino; //文件所有者的用户ID。
unsigned short st_mode; //文件类型和访问权限。
short st_nlink; //文件所有者的组ID。
short st_uid; //文件所有者的用户ID。
short st_gid; //表示文件或目录的所有者的组ID
//_dev_t 是一个用于表示设备ID的数据类型,它通常是一个整数类型。
//对于普通文件和目录,st_rdev 的值通常是0,因为它不是一个特殊文件。
//对于设备文件(如字符设备或块设备),st_rdev 用于标识关联的设备。
_dev_t st_rdev; // 表示特殊文件(如设备文件)的设备ID
_off_t st_size; //文件所有者的用户ID。
time_t st_atime; //文件的最后访问时间。(秒级的时间戳)
time_t st_mtime; //文件的最后修改时间。(秒级的时间戳)
time_t st_ctime; //文件的最后状态改变时间(例如,权限变化)
};
//stat 获取文件info
struct stat file_info;
stat(path, &file_info);
example1:
int process_file(const char* path, const struct stat* sb, int typeflag){
if(typeflag == FTW_F){ //普通文件
std::cout << "file:" << path << std::endl;
}else if(typeflag == FTW_D){ //目录
std::cout << "Directory:" << path << std::endl;
}
return 0;
}
int main(){
const char *path = "./";
int result = ftw(path,process_file, 0);
if (result == -1) {
std::cerr << "failed" << std::endl;
return 1;
}
return 0;
}
限制递归层级
example2(只记录当前目录):
int process_file(const char* path, const struct stat* sb, int typeflag, struct FTW *ftwbuf){
if (ftwbuf->level > 1) {
// 如果目录深度大于0(即不在当前目录),
// 返回FTW_SKIPSUB以停止递归
// 返回FTW_CONTINUE继续
return FTW_CONTINUE;
}
if (typeflag == FTW_F) {
std::cout << "file:" << path << std::endl;
} else if (typeflag == FTW_D) {
std::cout << "Directory:" << path << std::endl;
}
return FTW_CONTINUE; // 返回0以继续遍历
}
void test_fwt(){
const char *path = "./";
int flags = FTW_PHYS;// 使用FTW_PHYS选项以跳过符号链接的解析
int result = nftw(path, process_file, 0, flags);
//resutl = -1 -> err
}
2. 使用opendir方法
opendir 标准C库函数(通用性好)
头文件dirent.h
遍历当前目录
void test_opendir(){
const char* path = "./";
DIR *dir = opendir(path);
if(dir == nullptr){
std::cerr << "open dir failed." << std::endl;
return;
}
struct dirent* entry;
while(((entry = readdir(dir)) != nullptr)){
if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0){
continue;
}
std::cout << "Name:" << entry->d_name << std::endl;
}
closedir(dir);
}
递归遍历子目录
void recursion_opendir(const char* path){
DIR *dir = opendir(path);
if(dir == nullptr){
std::cerr << "open dir failed." << std::endl;
return;
}
struct dirent* entry;
while(((entry = readdir(dir)) != nullptr)){
if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0){
continue;
}
std::cout << "Name:" << entry->d_name << std::endl;
char path1[1024];
snprintf(path1, sizeof(path1), "%s/%s", path, entry->d_name);
struct stat info;
if (stat(path1, &info) == 0) {
if (S_ISDIR(info.st_mode)) {
std::cout << "Type: Directory" << std::endl;
recursion_opendir(path1);
} else if (S_ISREG(info.st_mode)) {
std::cout << "Type: Regular File" << std::endl;
}
}
}
closedir(dir);
}
3.c++ 17提供的新filesystem头文件
void traverseDir(const std::filesystem::path& root, int depth = 0){
try{
if(std::filesystem::is_directory(root) && std::filesystem::exists(root)){
for(const auto& entry:std::filesystem::directory_iterator(root)){
for(int i = 0; i< depth; i++){
std::cout << " ";
}
std::cout << entry.path().filename() << std::endl;
if(std::filesystem::is_directory(entry)){
traverseDir(entry, depth+1); // 递归遍历子目录
}
}
}else{
std::cout << "root is not directory or exists " << std::endl;
}
}catch(const std::exception &ex){
std::cerr << "exception:" << ex.what() << std::endl;
}
}
void test_filesystem(){
std::filesystem::path root("./");
traverseDir(root);
}
4 . Windows api 方式遍历目录
没有必要写啦