linux 遍历文件及文件夹,linux文件夹操作及递归遍历文件夹(示例代码)
文件夹相关函数介绍
//mkdir 函数创建文件夹
#include
#include
int mkdir(const char *pathname, mode_t mode);
//rmdir 删除文件夹
#include
int rmdir(const char *pathname);
//dopendir/fdopendir //打开文件夹
DIR是一个结构体,是一个内部结构,用来存储读取文件夹的相关信息。
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
//readdir 读文件夹
#include
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
#include
void rewinddir(DIR *dirp);
把文件夹指针恢复到文件夹的起始位置。
//telldir函数
#include
long telldir(DIR *dirp);
函数返回值是为文件夹流的当前位置,表示文件夹文件距开头的偏移量。
//seekdir
#include
void seekdir(DIR *dirp, long offset);seekdir表示设置文件流指针位置。
//closedir 关闭文件夹流
#include
#include
int closedir(DIR *dirp);
使用递归来遍历文件夹下的文件
#include
#include
#include
#include
#include
#include
#include
#include
#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 遍历文件及文件夹,linux文件夹操作及递归遍历文件夹(示例代码)相关推荐
- python3查找文件中指定字符串_Python3在指定路径下递归定位文件中出现的字符串...
[本文出自天外归云的博客园] 脚本功能:在指定的路径下递归搜索,找出指定字符串在文件中出现的位置(行信息). 用到的python特性: 代码如下: import os import sys __all ...
- sex 无需下载_js读取本地json格式文件数据的几种实现方法,内有vue读取json示例代码。...
方法一:通过getJSON实现 getJSON是jquery提供的读取json格式文件的方法 首先我们将html中引入jquery,可以通过百度CDN引入,代码如下: 然后就可以在script中使用g ...
- C/C++ 二叉树的非递归遍历(前序、中序、后序非递归遍历)
二叉树的非递归遍历C/C++实现: 非递归先序遍历代码: void PreOrderTraversal (struct tree* root) { //非递归先序遍历struct tree* t ...
- c语言中文件的只读属性,C/C++ 文件属性设置(隐藏、只读、加密等)(示例代码)
GetFileAttributes 获取文件属性函数的返回值 返回字段 返回值 属性类型 FILE_ATTRIBUTE_READONLY 1 只读 FILE_ATTRIBUTE_HIDDEN 2 隐藏 ...
- linux内核3.4 led驱动,JZ2440_V3_内核驱动程序_点亮一个LED灯(示例代码)
为了用内核驱动程序点亮一个LED灯,真是费尽周折,参考了韦东山的<嵌入式linux驱动开发完全手册>(以下简称<完全手册>)和网上很多人的blog,最终在@Carlos·Wei ...
- linux比较命令 -eq,linux条件判断:eq、ne、gt、lt、ge、le(示例代码)
命令简介: 条件判断: 如果用户不存在 添加用户,给密码并显示添加成功: 否则 显示如果已经没在,没有添加: 变量名称: 1.只能包含字母.数字和下划线,并且不能数字开头: 2.不应该跟系统中已有的环 ...
- linux 如何跳出循环函数,(三)Linux Shell编程——Shell常用命令(输出、判断、循环、函数、包含)(示例代码)...
3. 常用命令 3.1 输出 3.1.1 echo命令 echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串.命令格式: echo arg name="coding" ...
- linux svn添加新文件夹,linux svn 添加文件夹
安装doxygen 安装包 doxygen-1.7.4.linux.bin.tar.gz(可在官网下载) 命令: 1) tar xvfz doxygen-1.7.4.linux.bin.tar.gz ...
- Android 递归删除文件和文件夹
import java.io.File;/*** 操作文件帮助类* @author JRoger.*/ public final class OperationFileHelper {/*** 递归删 ...
最新文章
- wadl 的自动生成(cxf版本3.1.1)
- 云南省2021高考成绩排名查询,2020年云南高考成绩位次排名及一分一段表查询
- fastdfs 测试客户端_分布式文件服务 FastDFS (第一篇)
- UVA - 11846 Finding Seats Again (DFS搜索对象)
- 双步位移求解特征值matlab,数值分析——带双步位移的QR分解求特征值算法
- 总结一下最近面试经常被问到的问题
- python菜鸟教程100例-Python 基础教程 | 菜鸟教程
- Linux查看和修改IP地址
- 在一个字符串中找到第一个只出现一次的字符, 并返回它的位置
- 项目建议书模板doc_简历模板大全,直接可以下载使用的简历模板
- xp系统的无线配置服务器,Windows XP系统下无线网卡配置及安装
- 网络安全等级保护云计算安全防护技术体系设计
- Windows10系统重装后必不可少的优化步骤
- Android 使用adb查看和修改电池信息
- 【正点原子MP157连载】第二十八章 A7和M4联合调试-摘自【正点原子】STM32MP1 M4裸机CubeIDE开发指南
- 手机上能搭建mysql服务器_安卓手机搭建网站服务器【Php+Mysql+Lighttpd】 – 楚盟博客...
- u盘Linux、window双系统文件格式安装
- win7系统软件安装显示NSIS ERROR的解决方法
- 值得看一眼的80句好话----转自徐小平博客
- win10电脑开机绕过密码自动登录,微软官方提供修改注册表跳过登录设置方法