linux文件夹操作及递归遍历文件夹
文件夹相关函数介绍
//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文件夹操作及递归遍历文件夹的更多相关文章
- Java File类应用:递归遍历文件夹和递归删除文件
要求: 1)采用递归遍历文件夹下的所有文件,包括子文件夹下的文件 2)采用递归删除文件下的所有文件 注意: 以下递归删除文件的方法,只能删除文件,所有的文件夹都还会存在 若要删除正文文件夹,可以在递归 ...
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
本文收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归删除文件夹及文件.指定文件夹下 面的所有内容copy到目标文件夹下面.指定文件夹下面的所有内 ...
- c# 封装的文件夹操作类之复制文件夹
c# 封装的文件夹操作类之复制文件夹 一.复制文件夹原理: 1.递归遍历文件夹 2.复制文件 二.FolderHelper.cs /// <summary> /// 文件夹操作类 /// ...
- MFC_选择目录对话框_选择文件对话框_指定目录遍历文件
选择目录对话框 void C资源共享吧视频广告清理工具Dlg::OnBnClickedCls() { // 清空编辑框内容 m_Edit.SetWindowTextW(L""); ...
- TypeScript ES6-Promise 递归遍历文件夹中的文件
貌似很多人都爱用这个作为写文章的初尝试,那来吧.遍历文件夹下的所有文件,如遍历文件夹下并操作HTML/CSS/JS/PNG/JPG步骤如下:1.传入一个路径,读取路径里面所有的文件:2.遍历读取的文件 ...
- [C#]递归遍历文件夹
/// <summary> /// 递归获取文件夹目录下文件 /// </summary> /// <param name="pathName"> ...
- java File基本操作,以及递归遍历文件夹
java 的文件操作,相对来说是比较重要的,无论是编写CS还是BS程序,都避免不了要与文件打交道,例如读写配置文件等.虽然现在很多框架都直接帮你做好了这一步! java.io.File 底层是调用与c ...
- Python【day 14-2】递归遍历文件夹
#需求 遍历文件夹中所有的子文件夹及子文件--用递归实现 '''''' ''' 伪代码 1.遍历根目录--listdir for 得到第一级子文件夹(不包含子文件夹的子文件)和文件 2.判断是文件还是 ...
- XML DTD约束 对xml文件的crud的查询Read Retrieve操作 xml递归遍历
本地的dtd文档 xml中引入dtd文档 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE 书 ...
随机推荐
- npm更新包
方法一手动跟新: 手动修改package.json中依赖包版本,执行npm install --force,强制从远程下载所有包更新本地包 方法二使用第三方插件: npm install -g npm ...
- Spark streaming技术内幕6 : Job动态生成原理与源码解析
原创文章,转载请注明:转载自 周岳飞博客(http://www.cnblogs.com/zhouyf/) Spark streaming 程序的运行过程是将DStream的操作转化成RDD的操作,S ...
- 【转载】文件下载FileDownloader
原文地址:https://github.com/lingochamp/FileDownloader 特点 简单易用 高并发 灵活 可选择性支持: 独立/非独立进程 自动断点续传 需要注意 当下载的文件 ...
- Luogu P2590 树的统计(树链剖分+线段树)
题意 原文很清楚了 题解 重链剖分模板题,用线段树维护即可. #include <cstdio> #include <cstring> #include <algorit ...
- BZOJ 4873 寿司餐厅(最大权闭合图 网络流)
寿司餐厅 时间限制: 1 Sec 内存限制: 512 MB提交: 6 解决: 3[提交][状态][讨论版] 题目描述 Kiana 最近喜欢到一家非常美味的寿司餐厅用餐.每天晚上,这家餐厅都会按顺序 ...
- Linux修改用户基本信息(不含密码)
如果想修改密码请查看Linux命令之passwd.chpasswd (1).使用usermod修改用户基本信息 Linux命令之usermod (2).进入配置文件修改用户信息 使用vim /etc/ ...
- RabbitMQ (十一) 消息确认机制 - 消费者确认
由于生产者和消费者不直接通信,生产者只负责把消息发送到队列,消费者只负责从队列获取消息(不管是push还是pull). 消息被"消费"后,是需要从队列中删除的.那怎么确认消息被&q ...
- Line Reflection -- LeetCode
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...
- 【推导】计蒜客17116 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 C. Sum
题意:S(x)被定义为x的十进制表示下的数位之和.给你x,让你找一个正整数k,使得S(kx)能被233整除.k不超过2千位. 由于x不超过1000000,不论x是多少,10000000重复233次一定 ...
- (原创)Stanford Machine Learning (by Andrew NG) --- (week 1) Introduction
最近学习了coursera上面Andrew NG的Machine learning课程,课程地址为:https://www.coursera.org/course/ml 在Introduction部分 ...