linux文件目录部分操作

#include <sys/stat.h>#include <unistd.h>#include <dirent.h>//创建文件夹 路径 掩码 int mkdir(const char *path, mode_t mode); 
// 获取当前工作路径 buf用于接受路径缓存
char *getcwd(char *buf, size_t size);
// 进入文件夹 和cd一样
int  chdir(const char *path);

//打开路径并建立子目录流,返回子目录流指针
DIR *opendir(const char *filename);
//读取子目录流结构
struct dirent *readdir(DIR *dirp);
//函数返回值里记录着子目录流的当前位置
long telldir(DIR *dirp);
//对dir指定的子目录流中的目录数据项的指针进行设置,loc的值用来设置指针位置,他应该通过telldir调用获得。
void seekdir(DIR *dirp, long loc);
//关闭子目录流
int closedir(DIR *dirp);

dirent 结构体(举个例子,只是其中一种):

struct dirent{             /* when _DARWIN_FEATURE_64_BIT_INODE is NOT defined */
  ino_t d_ino;                   /* file number of entry */
  __uint16_t d_reclen;             /* length of this record */
  __uint8_t d_type;                /* file type, see below */
  __uint8_t d_namlen;              /* length of string in d_name */
  char d_name[255+1];          /* name must be no longer than this */
};

针对linux文件目录部分操作举个例子:
dir.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>

/*    
 *  function:    scan_dir             扫描当前 文件夹 下面的文件或文件夹
 *  param1:      const char *dir      文件夹路径
 *  param2:      int depth            记录文件夹深度,作用于打印时候的排版
 *  return:      void
 */
void scan_dir(const char *dir, int depth)
{

    //1.声明文件夹描述符和结构体等
    DIR *dp;                //文件夹描述符
    struct dirent *entry;    //文件夹结构体
    struct stat statbuff;    //文件属性结构体

    if(dir == NULL)            //先判断传参是否正确
    {
        puts("please in put dir_path");
        return;
    }

    //2. 打开文件夹
    dp = opendir(dir);
    if(dp == NULL)
    {
        puts("cant open this dir");
        return;
    }

    //3. 进入该文件夹
    chdir(dir);

    //4.readdir会返回当前文件夹下面的子流信息,并且指针会指向下一个文件,
    //每次返回的信息都保存在dirent的结构体指针中,每读完一个文件指针就会指向下一个文件,所以,读取最后一个文件时,因为没有下一个文件,返回指针为NULL
    //利用这一特性,在扫描文件夹的时候,可以使用while循环。
    while((entry = readdir(dp)) != NULL){

        lstat(entry->d_name,&statbuff); //获取文件属性    ,entry->d_name 保存的就是文件路径
        
        if(statbuff.st_mode & S_IFDIR)    //判断是否是文件夹,如果是文件夹,就使用递归函数
        {
            if( strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name,"..") == 0)    //跳过 . 和 .. 文件
            {
                continue;
            }
            printf(">%*s%s/
",depth,"", entry->d_name); //将本次读取的文件夹名字打印出来
            //scan_dir(entry->d_name,depth+4);                //如果打开这个注释,将会进入所有子文件夹,因为这是递归操作

        }else{                            //如果不是文件夹,就直接输出文件名
            printf("%*s%s
",depth, "",entry->d_name );
        }

    }
        chdir("..");    
        closedir(dp);    //最后关闭文件夹

}

dir.h

#ifndef __DIR_H
#define __DIR_H

void scan_dir(const char *dir, int depth);

#endif

main.c

#include <stdio.h>
#include <string.h>

#include "dir.h"

int main(int argc, char const *argv[])
{
    puts("scan /Users dir");
    scan_dir("/Users/ins/ke",0); //文件夹路径根据个人随便定, 如果是系统文件夹要用sudo 来执行程序
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追梦偏执狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值