文件夹相关函数介绍

//mkdir 函数创建文件夹

#include <sys/stat.h>
#include <sys/types.h> int mkdir(const char *pathname, mode_t mode);

//rmdir 删除文件夹

#include <unistd.h>
int rmdir(const char *pathname);

//dopendir/fdopendir  //打开文件夹

DIR是一个结构体,是一个内部结构,用来存储读取文件夹的相关信息。

DIR *opendir(const char *name);
DIR *fdopendir(int fd);

//readdir 读文件夹

#include <dirent.h>
struct dirent *readdir(DIR *dirp); struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supportedby all file system types */
char d_name[256]; /* filename */
};

readdir 每次返回一条记录项。。DIR*指针指向下一条记录项。

//rewinddir

#include <sys/types.h>
#include <dirent.h> void rewinddir(DIR *dirp);

把文件夹指针恢复到文件夹的起始位置。

//telldir函数

 #include <dirent.h>

 long telldir(DIR *dirp);

函数返回值是为文件夹流的当前位置,表示文件夹文件距开头的偏移量。

//seekdir

#include <dirent.h>
void seekdir(DIR *dirp, long offset);

seekdir表示设置文件流指针位置。

//closedir 关闭文件夹流

 #include <sys/types.h>
#include <dirent.h> int closedir(DIR *dirp);

使用递归来遍历文件夹下的文件

#include<stdio.h>
#include <errno.h>
#include<stdlib.h>
#include<string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> #define MAX_PATH 512 void print_file_info(char *pathname);
void dir_order(char *pathname); void dir_order(char *pathname)
{
DIR *dfd;
char name[MAX_PATH];
struct dirent *dp;
if ((dfd = opendir(pathname)) == NULL)
{
printf("dir_order: can't open %s\n %s", pathname,strerror(errno));
return;
}
while ((dp = readdir(dfd)) != NULL)
{
if (strncmp(dp->d_name, ".", 1) == 0)
continue; /* 跳过当前文件夹和上一层文件夹以及隐藏文件*/
if (strlen(pathname) + strlen(dp->d_name) + 2 > sizeof(name))
{
printf("dir_order: name %s %s too long\n", pathname, dp->d_name);
} else
{
memset(name, 0, sizeof(name));
sprintf(name, "%s/%s", pathname, dp->d_name);
print_file_info(name);
}
}
closedir(dfd); }
void print_file_info(char *pathname)
{
struct stat filestat;
if (stat(pathname, &filestat) == -1)
{
printf("cannot access the file %s", pathname);
return;
}
if ((filestat.st_mode & S_IFMT) == S_IFDIR)
{
dir_order(pathname);
}
printf("%s %8ld\n", pathname, filestat.st_size);
}
int main(int argc, char *argv[])
{
if (argc == 1)
{
dir_order(".");
} else
{
dir_order(argv[1]);
}
return 0;
}

linux文件夹操作及递归遍历文件夹的更多相关文章

  1. Java File类应用:递归遍历文件夹和递归删除文件

    要求: 1)采用递归遍历文件夹下的所有文件,包括子文件夹下的文件 2)采用递归删除文件下的所有文件 注意: 以下递归删除文件的方法,只能删除文件,所有的文件夹都还会存在 若要删除正文文件夹,可以在递归 ...

  2. C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....

    本文收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归删除文件夹及文件.指定文件夹下 面的所有内容copy到目标文件夹下面.指定文件夹下面的所有内 ...

  3. c# 封装的文件夹操作类之复制文件夹

    c#  封装的文件夹操作类之复制文件夹 一.复制文件夹原理: 1.递归遍历文件夹 2.复制文件 二.FolderHelper.cs /// <summary> /// 文件夹操作类 /// ...

  4. MFC_选择目录对话框_选择文件对话框_指定目录遍历文件

    选择目录对话框 void C资源共享吧视频广告清理工具Dlg::OnBnClickedCls() { // 清空编辑框内容 m_Edit.SetWindowTextW(L""); ...

  5. TypeScript ES6-Promise 递归遍历文件夹中的文件

    貌似很多人都爱用这个作为写文章的初尝试,那来吧.遍历文件夹下的所有文件,如遍历文件夹下并操作HTML/CSS/JS/PNG/JPG步骤如下:1.传入一个路径,读取路径里面所有的文件:2.遍历读取的文件 ...

  6. [C#]递归遍历文件夹

    /// <summary> /// 递归获取文件夹目录下文件 /// </summary> /// <param name="pathName"> ...

  7. java File基本操作,以及递归遍历文件夹

    java 的文件操作,相对来说是比较重要的,无论是编写CS还是BS程序,都避免不了要与文件打交道,例如读写配置文件等.虽然现在很多框架都直接帮你做好了这一步! java.io.File 底层是调用与c ...

  8. Python【day 14-2】递归遍历文件夹

    #需求 遍历文件夹中所有的子文件夹及子文件--用递归实现 '''''' ''' 伪代码 1.遍历根目录--listdir for 得到第一级子文件夹(不包含子文件夹的子文件)和文件 2.判断是文件还是 ...

  9. XML DTD约束 对xml文件的crud的查询Read Retrieve操作 xml递归遍历

    本地的dtd文档 xml中引入dtd文档 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE 书 ...

随机推荐

  1. NoSQL 数据库应用

      类型 部分代表 特点 列存储 Hbase Cassandra Hypertable 顾名思义,是按列存储数据的.最大的特点是方便存储结构化和半结构化数据,方便做数据压缩,对针对某一列或者某几列的查 ...

  2. Java中HashMap(泛型嵌套)的遍历

    //Studnet package yzhou.gen03; public class Student<T> { private T score; public T getScore() ...

  3. 17-7-20-electron中主进程和渲染进程区别与通信

    老规矩,先吐槽,再记录. 今天被上司教育了将近一个小时.因为之前自动更新的模块,我认为已经完成了,但是还有一些细节没有完善好,就一直一直的被教育~ 事情全部做完,提交以后关闭issue! electr ...

  4. Python开发基础-Day7-闭包函数和装饰器基础

    补充:全局变量声明及局部变量引用 python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 global关键字用来在函数或其 ...

  5. JZYZOJ1502 [haoi2008]下落的圆盘 计算几何 贪心

    http://172.20.6.3/Problem_Show.asp?id=1502这种题用了快一天才写出来也是真的辣鸡.主要思路就是计算一下被挡住的弧度然后对弧度进行贪心.最开始比较困扰的是求弧度值 ...

  6. BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡(贪心)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3399 [题目大意] 将一个集合调整成另一个集合中的数,把一个数+1需要消耗x,-1需要 ...

  7. 监视scrollview是否滚动到底

    //监视scrollview是否滚动到底 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ [refreshHeaderViewegoRe ...

  8. Java学习笔记(15)

    iterator方法 迭代器的作用:就是用于抓取集合中的元素 注:迭代器返回的一个接口类型的实现类,是一种多态的用法,而不是接口在调用方法 public class Demo2 { public st ...

  9. Nginx配置自签名的SSL证书(转载)

    要保证Web浏览器到服务器的安全连接,HTTPS几乎是唯一选择.HTTPS其实就是HTTP over SSL,也就是让HTTP连接建立在SSL安全连接之上. SSL使用证书来创建安全连接.有两种验证模 ...

  10. POJ 3057 Evacuation 二分+最大流

    Evacuation 题目连接: http://poj.org/problem?id=3057 Description Fires can be disastrous, especially when ...