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 ...
随机推荐
- 杭电OJ——1007 Quoit Design(最近点对问题)
Quoit Design Problem Description Have you ever played quoit in a playground? Quoit is a game in whic ...
- MFC 之 截图工具
这个截图工具能实现最主要的截图功能,并保存为bmp图片. 编写环境是vs2005,使用Unicode,基于对话框. 没什么难度,直接看代码 项目名称为CutOut // CutOutDlg.h : 头 ...
- static在C和C++中的用法和区别
static主要有三个作用: (1)局部静态变量 (2)外部静态变量/函数 (3)静态数据成员/成员函数 前两种C和C++都有,第三种仅在C++中有,下面分别作以下介绍: 一.局部静态变量 在C/C+ ...
- JQuery5.04获取
获取body: $('body'); 或者 $(document.body); 获取元素标签:$('div'); $('a'); 获取ID: $('id'); 获取某个元素的某个属性: $('a ...
- [置顶] Android框架攻击之Fragment注入
为了适应越来越大的设备屏幕,Android在3.X后引入了Fragment概念,作用是可以在一个屏幕上同时显示多个Activity,以达到充分利用屏幕的目的.关于Fragment的使用说明,可以阅读& ...
- form里两个submit按钮,在onsubmit中判断哪个被点
记下别人的解决方法(有效): 方法1:(已验证过) <form name="form1" onsubmit="show()"> ..... < ...
- 基于visual Studio2013解决面试题之0703翻转栈
题目
- webview加载网页加载不出来
1.webView.loadUrl(picTargetUrl); 写在最前面. 1.在无线城市迷你版的项目,用webview去loadUrl的时候出现加载的现象. url 地址是 http://go. ...
- Asp.NET调用百度翻译
Asp.NET调用百度翻译,图示: HTML: <%@ Page Language="C#" AutoEventWireup="true" CodeFil ...
- spring mvc ModelAndView向前台传值
今天在做项目的时候遇到一个问题,把第一个页面保存的id传到第三个页面中去用,原来是在controller层加了一个全局变量控制的,但是后来发现这个变量实现不了我要的功能,于是查了一下,原来ModelA ...