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 */
};

举个例子

>> dir.c << 

//1. opendir()
//2. cddir()
//3. reader()
//4. closed()
#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;
}

2. 删除目录或文件操作

#include <unistd.h>
//删除文件夹
int rmdir(const char *path);
//删除文件
int unlink(const char *path);
原文地址:https://www.cnblogs.com/kmist/p/10632512.html