转载请注明原创: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. echarts问题

    1.鼠标经过折线图  显示的框中的文字设置,需要设置tooltip下的formatter属性 formatter属性值可以为字符串也可function formatter:function(data) ...

  2. 鲜为人知的 Python 语法

    所有人(好吧,不是所有人)都知道 python 是一门用途广泛.易读.而且容易入门的编程语言.   但同时 python 语法也允许我们做一些很奇怪的事情.   使用 lambda 表达式重写多行函数 ...

  3. 情绪ABC理论

    美国著名心理学家阿尔伯特·艾利斯 [Albert Ellis 1913.09.27]于20世纪50年代创立, 其理论认为引起人们情绪困扰的并不是外界发生的事件,而是人们对事件的态度.看法.评价等认知内 ...

  4. 3282. Tree【LCT】

    Description 给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...

  5. 【转】Android业务组件化之URL Scheme使用

    前言: 最近公司业务发展迅速,单一的项目工程不再适合公司发展需要,所以开始推进公司APP业务组件化,很荣幸自己能够牵头做这件事,经过研究实现组件化的通信方案通过URL Scheme,所以想着现在还是在 ...

  6. 【转】 iOS播放视频时候,忽略设备静音按钮

    用户有时会在静音模式下观看视频,如果不主动设置的话,视频是没有声音的,通过AVAudioSession可开启以视频为主导的播放模式, 首先需要导入,AVFoundtion.framework,在控制播 ...

  7. Hive学习之路 (三)Hive元数据信息对应MySQL数据库表

    概述 Hive 的元数据信息通常存储在关系型数据库中,常用MySQL数据库作为元数据库管理.上一篇hive的安装也是将元数据信息存放在MySQL数据库中. Hive的元数据信息在MySQL数据中有57 ...

  8. Plugin with id 'com.novoda.bintray-release' not found的解决方法

    我们一般在在github上下载下来的代码,有时候会提示Plugin with id ‘com.novoda.bintray-release’ not found的错误,这个怎么解决呢,其实很简单,只要 ...

  9. 【转】matplotlib制图——图例legend

    转自:https://www.cnblogs.com/alimin1987/p/8047833.html import matplotlib.pyplot as pltimport numpy as ...

  10. Linux系统管理命令

    Linux系统管理命令 命令 说明 stat 显示指定文件的相关信息,比ls命令显示内容更多 who 显示在线登录用户 hostname 显示主机名称 uname 显示系统信息 top 显示当前系统中 ...