转载请注明原创: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()函数遍历目录的更多相关文章

  1. [C++]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. didEndEditingRowAtIndexPath with nil indexPath

    在UITableViewController中,通过滑动删除按钮删除一行,首先收到Table view data source call: tableView:commitEditingStyle:f ...

  2. http://blog.163.com/db_teacher/blog/static/194540298201110723712407/

    实验(七).第五章 数据库完整性 2011-11-07 14:37:12|  分类: 默认分类 |  标签: |字号大中小 订阅     一.实验目的 1.熟悉通过SQL语句对数据进行完整性控制 2. ...

  3. dojox.grid.EnhancedGrid 和 dojox.grid.DataGrid 的继承关系

    dojox.grid.EnhancedGrid  的介绍说, EnhancedGrid 是基于 DataGrid 提供增强功能的. EnhancedGrid (dojox.grid.EnhancedG ...

  4. java文件(文件夹)操作

    java中文件操作 判断是否为文件file.isFile()方法 判断是否为目录file.isDirectory()方法 判断是否存在file.exist()方法 创建新文件file.createNe ...

  5. java 中读取本地文件中字符

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  6. unity3d触屏操作对象运动

    using UnityEngine; using System.Collections; public class robot : MonoBehaviour { private GameObject ...

  7. HNC-局部联想脉络

    局部联想脉络 概念分为:抽象概念.具体概念 对抽象概念用 五元组 和 语义网络 表达 对具体概念用 挂靠展开近似 表达 五元组:动态.静态.属性.值.效应.(u,g,u,z,r),用于表达抽象概念的外 ...

  8. Java Numeric Formatting--reference

    I can think of numerous times when I have seen others write unnecessary Java code and I have written ...

  9. ref传参时出错

    下面是一段正确的代码: //基类A public class A { public void SomeMethod(A a) { } } //派生类B public class B:A { } //在 ...

  10. ASP.NET-FineUI开发实践-4(二)

    在网上找了找,实验了一下window弹出和关闭的动画效果分享一下. 接上次的代码 default.js window_tips.animCollapse= true;//打开动画效果 window_t ...