Linux 多线程目录遍历

目录遍历函数#include #include #include char *getcwd(char *buf, size_t size);//获取当前目录的绝对路径DIR *opendir(const char *name); //获取一个路径的目录流DIRDIR *fdopendir(int fd);
摘要由CSDN通过智能技术生成

目录遍历函数

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
char *getcwd(char *buf, size_t size);//获取当前目录的绝对路径
DIR *opendir(const char *name);      //获取一个路径的目录流DIR
DIR *fdopendir(int fd);              //功能同上,只是路径换成文件描述符
struct dirent *readdir(DIR *dirp);   //读取目录流中的一个目录
struct dirent
{
    ino_t          d_ino;       /* inode number */
    char           d_name[256]; /* filename */
};
char* dirname(char* pathname);     //获取路径的父目录
char* basename(char* pathname);    //获取文件名
int stat(const char *path, struct stat *buf);//将当前文件的信息放入stat结构体中
struct stat {
    dev_t     st_dev;     /* ID of device containing file */
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* protection */
    nlink_t   st_nlink;   /* number of hard links */
    uid_t     st_uid;     /* user ID of owner */
    gid_t     st_gid;     /* group ID of owner */
    dev_t     st_rdev;    /* device ID (if special file) */
    off_t     st_size;    /* total size, in bytes */
    blksize_t st_blksize; /* blocksize for file system I/O */
    blkcnt_t  st_blocks;  /* number of 512B 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 */
};

目录遍历流程

  • 首先通过 getcwd(NULL,0)获取当前目录的绝对路径
   char *getcwd(char *buf, size_t size);
  • 然后用目录指针DIR* 返回打开目录后的目录指针
   DIR *opendir(const char* path);
  • 用struct dirent* 返回目录的具体信息的指针
   struct dirent *readdir(DIR *dirp);;
  • 用stat函数提取文件的具体信息

                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值