转载请注明原创: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 ;
}

  

[C++]linux下实现ls()函数遍历目录的更多相关文章

  1. linux下实现ls()函数遍历目录

    转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600794.html 需求:在linux下遍历目录,输出目录中各文件名. 在linux下遍历目录的相关函数有 ...

  2. Linux 下的权限改变与目录配置

    Linux 下的权限改变与目录配置 ./代表本目录的意思. (1):用户与用户组, 1:文件所有者,文件被某一用户所有 2:用户组:    对文件给与一个或者多个用户权限配置 3:其它人: (2):l ...

  3. linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制)

    linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制) 发表于2012//07由feng linux 本身的ugo rwx的权限,对于精确的权限控制很是力不从心的,ac ...

  4. 实现linux下的ls

    实现linux下的ls ls的使用 ls -a 列出文件下所有的文件,包括以"."开头的隐藏文件(linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录). ls -l ...

  5. linux下nginx服务器域名指定目录

    一般,域名指定ip之后,需要在ip所在的机器去指定相应站点的目录,否则域名会不起作用: 下面说说linux下的nginx服务器指定目录的细节: 域名绑定目录的配置文件都放到这里: /usr/local ...

  6. Linux下利用ioctl函数获取网卡信息

    linux下的ioctl函数原型如下: #include <sys/ioctl.h> int ioctl(int handle, int cmd, [int *argc, int argv ...

  7. 实现Linux下的ls -l命令

    基本实现了Linux下的ls -l命令,对于不同的文件显示不同的颜色和显示符号链接暂时没有实现: /************************************************** ...

  8. Linux 下mysql修改数据库存放目录方法和可能遇到的问题

    MySQL版本:5.6.23-enterprise-commercial-advanced ,使用rpm安装linux:Red Hat Enterprise Linux Server release ...

  9. 【C/C++】Linux下使用system()函数一定要谨慎

    [C/C++]Linux下使用system()函数一定要谨慎 http://my.oschina.net/renhc/blog/53580 曾经的曾经,被system()函数折磨过,之所以这样,是因为 ...

随机推荐

  1. 【openjudge】【字符串+模拟】1777:文件结构“图”

    [题目传送门:]戳 [描述:] 在计算机上看到文件系统的结构通常很有用.Microsoft Windows上面的"explorer"程序就是这样的一个例子.但是在有图形界面之前,没 ...

  2. 64. [Mcoi2018]终末之诗(上)

    Description 求出\(k^{k^{k^{k^{...}}}} \pmod p\) 的结果 扩展欧拉定理:\[a^x=a^{min(x,x\%\varphi(p)+\varphi(p))}(m ...

  3. c++ 堆和栈以及区别

    c++中内存分成5个区:堆.栈.自由存储区.全局\静态存储区.常量存储区 栈是一种连续存储的数据结构,具有先进后出的性质.堆是一种非连续的树形存储数据结构,每个节点有一个值,整棵树是经过排序的,特点是 ...

  4. h5py

    解决办法: sudo apt-get install libhdf5-dev sudo apt-get install python-h5py

  5. Java泛型学习一

    Java泛型 所谓泛型,就是变量类型的参数化.泛型是java1.5中引入的一个重要特征,通过引入泛型,可以使编译时类型安全,运行时更少抛出ClassCastException的可能.一提到参数化,最熟 ...

  6. webStorm常用设置之过滤文件夹

    webStorm是一款很好用的前端IDE,但是不能否认的是webStorm很重,没有sublime轻便 尤其是项目cnpm后,加载node_modules时的状态,简直想那块豆腐自杀有莫有,就算你耐心 ...

  7. 【gulp】Gulp的安装和配置 及 系列插件

    注意:要安装俩次gulp(全局和本地):全局安装gulp是为了执行gulp任务,本地安装gulp则是为了调用gulp插件的功能. 之前由大牛帮忙配置的gulp来用.今天时间充裕,就和小伙伴一起动手配置 ...

  8. Node学习笔记之模块实现

    一.模块分类 由Node提供的模块,称为核心模块:部分核心模块在Node源代码的编译过程中,编译进了二进制执行文件.在node进程启动时,该部分就直接加载进内存,文件定位和编译执行的步骤可以省略掉,并 ...

  9. head 标签里有什么?

    head 标签里有什么? 每一个 HTML 文档中,都有一个不可或缺的标签:<head> ,它作为一个容器,主要包含了用于描述 HTML 文档自身信息(元数据)的标签,这些标签一般不会在页 ...

  10. C语言第四次实验

    这次实验共计7道题目 以下代码亲测无误 1.用选择排序法,键盘输入10个整数,对10个整数进行排序(升序) 1.第一种思路就是常规思路,输入--排序--输出 源代码如下: //常规思路,输入,排序,输 ...