Linux c/c++ 遍历指定目录下的文件

您所在的位置:网站首页 linuxC遍历文件名 Linux c/c++ 遍历指定目录下的文件

Linux c/c++ 遍历指定目录下的文件

2023-09-11 05:49| 来源: 网络整理| 查看: 265

struct dirent { long d_ino; /* inode number 索引节点号 / off_t d_off; / offset to this dirent 在目录文件中的偏移 / unsigned short d_reclen; / length of this d_name 文件名长 / unsigned char d_type; / the type of d_name 文件类型 / char d_name [NAME_MAX+1]; / file name (null-terminated) 文件名,最长255字符 */ }

struct stat { dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/ ino_t st_ino; /* inode number -inode节点号*/ mode_t st_mode; /* protection -保护模式?/ nlink_t st_nlink; / number of hard links -链向此文件的连接数(硬连接)/ uid_t st_uid; / user ID of owner -user id*/ gid_t st_gid; /* group ID of owner - group id*/ dev_t st_rdev; /* device ID (if special file) -设备号,针对设备文件*/ off_t st_size; /* total size, in bytes -文件大小,字节为单位*/ blksize_t st_blksize; /* blocksize for filesystem I/O -系统块的大小*/ blkcnt_t st_blocks; /* number of blocks allocated -文件所占块数*/ time_t st_atime; /* time of last access -上次存取时间*/ time_t st_mtime; /* time of last modification -上次修改时间*/ time_t st_ctime; /* time of last status change - 创建时间*/ };

S_ISLNK(st_mode)是否是一个连接. S_ISREG(st_mode)是否是一个常规文件. S_ISDIR(st_mode)是否是一个目录 S_ISCHR(st_mode)是否是一个字符设备. S_ISBLK(st_mode)是否是一个块设备 S_ISFIFO(st_mode)是否是一个FIFO文件. S_ISSOCK(st_mode)是否是一个SOCKET文件. enum { DT_UNKNOWN = 0, //未知类型

define DT_UNKNOWN DT_UNKNOWN DT_FIFO = 1, //管道 define DT_FIFO DT_FIFO DT_CHR = 2, //字符设备 define DT_CHR DT_CHR DT_DIR = 4, //目录 define DT_DIR DT_DIR DT_BLK = 6, //块设备 define DT_BLK DT_BLK DT_REG = 8, //常规文件 define DT_REG DT_REG DT_LNK = 10, //符号链接 define DT_LNK DT_LNK DT_SOCK = 12, //套接字 define DT_SOCK DT_SOCK DT_WHT = 14 //链接 # define DT_WHT DT_WHT

}

#include #include #include #include #include #include #include #include using namespace std; bool find_file_under_dir(const string &AbsFilePath,vector &FileName) { DIR *dir; struct dirent *ptr; if(!(dir = opendir(AbsFilePath.c_str()))) { cout continue; } FileName.push_back(string(ptr->d_name)); } closedir(dir); return true; } int main(int argc,char *argv[]) { vector FileName; if(find_file_under_dir("/home/cqc/share",FileName)) { for(size_t i = 0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3