LINUX编程学习笔记(十三) 遍历目录的两种方法
1 默认情况下 实际用户和有效用户是一样的
实际用户:执行用户
有效用户:权限用户
getuid() 实际用户
geteuid() 有效用户
chmod u+s 之后 ,其他人执行文件时,实际用户和有效用户会不一样
2 目录相关函数
int chdir(const char *path);改变当前目录
int mkdir(const char *pathname, mode_t mode); 创建目录
int rmdir(const char *pathname); 删除目录
int unlink(const char *pathname); 删除文件
mode_t umask(mode_t mask); 设置文件权限屏蔽位
stat fstat lstat文件目录状态
3 目录的遍历
3.1 方法一 opendir + readdir
DIR *opendir(const char *name);
struct dirent *readdir(DIR *dirp);
int closedir(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 supported
by all file system types */
char
d_name[256]; /* filename */
};
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>//exit() int main()
{
DIR *d = opendir("/home/zhao");
if(d == 0)
{
perror("opendir:%m\n");
exit(1);
} struct dirent * de;
while(de=readdir(d))
{
printf("%s \t%d\n",de->d_name,de->d_type);
}
//d_type 4 表示目录 8表示文件 closedir(d); }
3.2 方法2 scandir
int scandir(const char *dirp, //目录名
struct dirent ***namelist, //返回目录列表
int (*filter)(const struct dirent *), //回调函数 过滤目录 NULL表不过滤
int (*compar)(const struct dirent **, const struct dirent **)); //对查询结果排序 NULL表不排序
过滤规则 filter返回0 则不过滤掉 非0则显示
排序规则 compar >0 排在前面 <0排在后面
已有的排序
int alphasort(const void *a, const void *b);
int versionsort(const void *a, const void *b);
返回值: >=0 目录个数
-1 目录查找失败
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>//exit() int filter(const struct dirent *);
int sort(const struct dirent**,const struct dirent **); int main()
{
struct dirent **d;
//int r = scandir("/home/zhao",&d,filter,alphasort);
int r = scandir("/home/zhao",&d,filter,sort); //与alphasort你序
printf("子目录个数为%d\n",r);
while(*d != 0)
{
printf("%s\n",(*d)->d_name);
d++;
} return 0;
} //过滤掉名字以.开头的文件夹
int filter(const struct dirent* d)
{
if(strncmp(d->d_name,".",1) == 0)
{
return 0;
} return 1;
} int sort(const struct dirent**a,const struct dirent **b)
{
return -alphasort(a,b);
}
LINUX编程学习笔记(十三) 遍历目录的两种方法的更多相关文章
- linux系列之: 你知道查看文件空间的两种方法吗?
目录 简介 du命令 df命令 总结 简介 linux系统中查看文件空间大小应该是一个非常常见的命令了,今天给大家介绍linux系统中查看文件空间的两种方法和在使用中可能会遇到的奇怪问题. 为什么会有 ...
- python学习--python 连接SQLServer数据库(两种方法)
1. python 学习.安装教程参照: http://www.runoob.com/python/python-tutorial.html 2. 集成开发环境 JetBrains PyCharm C ...
- Linux 编程学习笔记----动笔makefile档
Befroe Beginning. 在设置暑假的plan ,关于Linux的书籍如今在看的是ALP和Linux高级程序设计(杨宗德)第三版.在计划中的是Linux高级环境编程. 如今開始关于Linux ...
- Linux编程学习笔记(二)
续上个章节,这个章节主要是Linux的远程登录系统操作笔记 一. Linux一般作为服务器使用,但是服务器都是在机房的,所以不可能经常跑到机房去操作系统,所以使用远程登录系统,在Linux的系统一般使 ...
- Linux 编程学习笔记----ANSI C 文件I/O管理
转载请注明出处:http://blog.csdn.net/suool/article/details/38129201 问题引入 文件的种类 依据数据存储的方式不同,能够将文件分为文本文件和二进制文件 ...
- LINUX编程学习笔记(十四) 创建进程与 父子进程内存空间
1什么是进程:进程是一个执行中的程序 执行的程序: 代码->资源->CPU 进程有很多数据维护:进程状态/进程属性 所有进程属性采用的一个树形结构体维护 ps -a//所有进程 ps - ...
- Linux 编程学习笔记----命令行参数处理
转载请注明出处.http://blog.csdn.net/suool/article/details/38089001 问题引入----命令行參数及解析 在使用linux时,与windows最大的不同 ...
- 链式前向星存树图和遍历它的两种方法【dfs、bfs】
目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...
- C51编程中对单片机绝对地址访问的两种方法
在进行8051单片机应用系统程序设计时,编程都往往少不了要直接操作系统的各个存储器地址空间.C51程序经过编译之后产生的目标代码具有浮动地址,其绝对地址必须经过BL51连接定位后才能确定.为了能够在C ...
随机推荐
- 先有Delphi内存对象,后有句柄(如果需要的话),最后再显示
在设计期放上一个Panel1和Button1,然后设置Panel1.Visible:=False 这时候执行: procedure TForm1.Button4Click(Sender: TObjec ...
- ARMv8 Linux内核head.S源码分析
ARMv8Linux内核head.S主要工作内容: 1. 从el2特权级退回到el1 2. 确认处理器类型 3. 计算内核镜像的起始物理地址及物理地址与虚拟地址之间的偏移 4. 验证设备树的地址是否有 ...
- CSDN-Code平台使用过程中的5点经验教训
昨天又创建了一个项目,fucms,可是本地一直没有权限提交,搞了非常久,试了几十次,都不行,我是非常的灰心和郁闷. 刚刚,和CSDN-Code的官方客服咨询了非常久非常久,最终摸索出来了一些心得体会 ...
- POJ 2250(最长公共子序列 变形)
Description In a few months the European Currency Union will become a reality. However, to join the ...
- Spring Session - Spring Boot
The completed guide can be found in the boot sample application. Updating Dependencies Before you us ...
- baas & API 网关
最近一段时间一直在做API 网关的工作.清晰看到当前云下Baas将会是主要方向,而API网关会是一把利剑. 本人正在规划API网关,有兴趣的可以一起探讨:hotwheels_bo@163.com
- php使用 _before_index() 来实现访问页面前,判断登录
C:\wamp\www\DEVOPS\Home\Lib\Action: <?php class IndexAction extends Action { function index(){ $t ...
- Android开发系列(二十八):使用SubMenu创建选项菜单
大部分手机上边都会有一个"MENU"键,在一个应用安装到手机上之后,能够通过"MENU"显示该应用关联的菜单. 可是,从Android 3.0開始,Androi ...
- 转换函数CONVERSION_EXIT_TSTRN_OUTPUT
CONVERSION_EXIT_TSTRN_OUTPUT 在路线表TVRO中字段TDVZND 运输提前时间,取出来的数值没有转换,需要此函数进行转换.如14400,000 转换后为14,400:00 ...
- 研究一下TForm.WMPaint过程(也得研究WM_ERASEBKGND)——TForm虽然继承自TWinControl,但是自行模仿了TCustomControl的全部行为,一共三种自绘的覆盖方法,比TCustomControl还多一种
先擦除背景: procedure TCustomForm.WMEraseBkgnd(var Message: TWMEraseBkgnd); begin if not IsIconic(Handle) ...