linux下实现ls()函数遍历目录
转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600794.html
需求:在linux下遍历目录,输出目录中各文件名。
在linux下遍历目录的相关函数有:
#include <dirent.h> DIR* opendir(const char* dir_path); struct dirent* readdir(DIR* dirp); int closedir(DIR* dirp); int lstat(const chat* filename,struct stat* st);
在这里涉及到几个结构体:DIR,struct dirent,struct stat:
DIR结构体是一个内部结构,类似与FILE,用来保存当前被读取的目录的信息:
truct __dirstream
{
void *__fd;
char *__data;
int __entry_data;
char *__ptr;
int __entry_ptr;
size_t __allocation;
size_t __size;
__libc_lock_define (, __lock)
}; typedef struct __dirstream DIR;
struct dirent,指向文件夹下的目录内容:
struct dirent
{
long d_ino; /* inode number 索引节点号 */
off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
unsigned short d_reclen; /* length of this d_name 文件名长 */
unsigned char d_type; /* the type of d_name 文件类型 */
char d_name [NAME_MAX+]; /* file name (null-terminated) 文件名,最长256字符 */
}
struct stat结构体保存文件信息,通过stat(),fstat(),lstat()函数返回,这三个函数的区别是:stat()传入文件路径得到stat,lstat()当传入的是符号链文件时,得到的是符号链文件的信息而不是符号链指向的文件,fstat()传入的是文件描述符而不是文件路径。
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
以下是ls()函数的全部实现,通过opendir()得到DIR*指针,readdir()函数获得指向struct dirent结构的指针,再通过struct stat得到每一个文件的信息。按字母顺序先输出目录文件名,再输出普通文件。
int ls(std::string path,std::string& ret)
{
DIR* dirp = opendir(path.c_str());
if(!dirp)
{
return -;
}
struct stat st;
struct dirent *dir;
std::vector<std::string> file_name;
std::vector<std::string> dir_name;
while((dir = readdir(dirp)) != NULL)
{
if(strcmp(dir->d_name,".") == ||
strcmp(dir->d_name,"..") == )
{
continue;
}
std::string full_path = path + dir->d_name;
if(lstat(full_path.c_str(),&st) == -)
{
continue;
}
std::string name = dir->d_name; //replace the blank char in name with "%$".
while(name.find(" ") != std::string::npos)
{
name.replace(name.find(" "),,"$%");
} if(S_ISDIR(st.st_mode)) //S_ISDIR()宏判断是否是目录文件
{
name += "[d]";
dir_name.push_back(name);
}
else
{
file_name.push_back(name);
}
} closedir(dirp); sort(file_name.begin(),file_name.end());
sort(dir_name.begin(),dir_name.end()); std::stringstream ss_ret;
int count = ; for(auto i=dir_name.begin();i!=dir_name.end();i++)
{
ss_ret<<*i;
count++;
if(count% == )
{
ss_ret<<std::endl;
}
else
{
ss_ret<<" ";
}
} for(auto i=file_name.begin();i!=file_name.end();i++)
{
ss_ret<<*i;
count++;
if(count% == ) //每五个名字回车。
{
ss_ret<<std::endl;
}
else
{
ss_ret<<" ";
}
} ret = ss_ret.str(); return ;
}
linux下实现ls()函数遍历目录的更多相关文章
- [C++]linux下实现ls()函数遍历目录
转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600794.html 需求:在linux下遍历目录,输出目录中各文件名. 在linux下遍历目录的相关函数有 ...
- Linux 下的权限改变与目录配置
Linux 下的权限改变与目录配置 ./代表本目录的意思. (1):用户与用户组, 1:文件所有者,文件被某一用户所有 2:用户组: 对文件给与一个或者多个用户权限配置 3:其它人: (2):l ...
- linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制)
linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制) 发表于2012//07由feng linux 本身的ugo rwx的权限,对于精确的权限控制很是力不从心的,ac ...
- 实现linux下的ls
实现linux下的ls ls的使用 ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录). ls -l ...
- linux下nginx服务器域名指定目录
一般,域名指定ip之后,需要在ip所在的机器去指定相应站点的目录,否则域名会不起作用: 下面说说linux下的nginx服务器指定目录的细节: 域名绑定目录的配置文件都放到这里: /usr/local ...
- Linux下利用ioctl函数获取网卡信息
linux下的ioctl函数原型如下: #include <sys/ioctl.h> int ioctl(int handle, int cmd, [int *argc, int argv ...
- 实现Linux下的ls -l命令
基本实现了Linux下的ls -l命令,对于不同的文件显示不同的颜色和显示符号链接暂时没有实现: /************************************************** ...
- Linux 下mysql修改数据库存放目录方法和可能遇到的问题
MySQL版本:5.6.23-enterprise-commercial-advanced ,使用rpm安装linux:Red Hat Enterprise Linux Server release ...
- 【C/C++】Linux下使用system()函数一定要谨慎
[C/C++]Linux下使用system()函数一定要谨慎 http://my.oschina.net/renhc/blog/53580 曾经的曾经,被system()函数折磨过,之所以这样,是因为 ...
随机推荐
- 【转】掌握java枚举类型(enum type)
原文网址:http://iaiai.iteye.com/blog/1843553 1 背景 在java语言中还没有引入枚举类型之前,表示枚举类型的常用模式是声明一组具有int常量.之前我们通常利用 ...
- List<T> 和DataTable的相互转换
我用的将集合类转换为DataTable 的方法 /// <summary> /// 将集合类转换成DataTable /// </summary> /// <param ...
- 实现字符串匹配的KMP算法
KMP算法是Knuth-Morris-Pratt算法的简称,它主要用于解决在一个长字符串S中匹配一个较短字符串s. 首先我们从整体来把我这个算法的思想. 字符串匹配的朴素算法: 我们容易想到朴素算法, ...
- puppet常用调试命令
yum快速部署puppet测试环境(C/S端) rpm -ivh http://yum.puppetlabs.com/puppetlabs-release-el-7.noarch.rpm yum r ...
- 最受欢迎linux命令
1. 以 root 帐户执行上一条命令 sudo !! 2. 利用 Python 搭建一个简单的 Web 服务器,可通过 http://$HOSTNAME:8000访问 python -m ...
- Controlling How NSThread and NSRunLoop Exit
http://shaheengandhi.com/controlling-thread-exit/ While most concurrency can be dealt with by using ...
- N - Optimal Milking - POJ 2112(二分图多重匹配+Floyd+二分搜索)
题意:有K太挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 分析:应该先使用floyd求出来点之间的最短路??(不晓得给 ...
- Column count of mysql.proc is wrong. Expected 20, found 16. Created with MySQL 50096, now running 50173.
IDEA链接mysql提示 Column count of mysql.proc is wrong. Expected 20, found 16. Created with MySQL 50096, ...
- SKPhysicsJointSliding类
继承自 NSObject 符合 NSCoding(SKPhysicsJoint)NSObject(NSObject) 框架 /System/Library/Frameworks/SpriteKit. ...
- android 回调机制实例!
详细实现为在类中定义接口.在接口的实现方法中传入參数(也能够不传). 在调用类中传入新建的接口.并实现未实现的方法. public class CallBackClass { //传入对应的接口作为參 ...