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编程学习笔记(十三) 遍历目录的两种方法的更多相关文章

  1. linux系列之: 你知道查看文件空间的两种方法吗?

    目录 简介 du命令 df命令 总结 简介 linux系统中查看文件空间大小应该是一个非常常见的命令了,今天给大家介绍linux系统中查看文件空间的两种方法和在使用中可能会遇到的奇怪问题. 为什么会有 ...

  2. python学习--python 连接SQLServer数据库(两种方法)

    1. python 学习.安装教程参照: http://www.runoob.com/python/python-tutorial.html 2. 集成开发环境 JetBrains PyCharm C ...

  3. Linux 编程学习笔记----动笔makefile档

    Befroe Beginning. 在设置暑假的plan ,关于Linux的书籍如今在看的是ALP和Linux高级程序设计(杨宗德)第三版.在计划中的是Linux高级环境编程. 如今開始关于Linux ...

  4. Linux编程学习笔记(二)

    续上个章节,这个章节主要是Linux的远程登录系统操作笔记 一. Linux一般作为服务器使用,但是服务器都是在机房的,所以不可能经常跑到机房去操作系统,所以使用远程登录系统,在Linux的系统一般使 ...

  5. Linux 编程学习笔记----ANSI C 文件I/O管理

    转载请注明出处:http://blog.csdn.net/suool/article/details/38129201 问题引入 文件的种类 依据数据存储的方式不同,能够将文件分为文本文件和二进制文件 ...

  6. LINUX编程学习笔记(十四) 创建进程与 父子进程内存空间

    1什么是进程:进程是一个执行中的程序 执行的程序: 代码->资源->CPU 进程有很多数据维护:进程状态/进程属性 所有进程属性采用的一个树形结构体维护 ps  -a//所有进程 ps - ...

  7. Linux 编程学习笔记----命令行参数处理

    转载请注明出处.http://blog.csdn.net/suool/article/details/38089001 问题引入----命令行參数及解析 在使用linux时,与windows最大的不同 ...

  8. 链式前向星存树图和遍历它的两种方法【dfs、bfs】

    目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...

  9. C51编程中对单片机绝对地址访问的两种方法

    在进行8051单片机应用系统程序设计时,编程都往往少不了要直接操作系统的各个存储器地址空间.C51程序经过编译之后产生的目标代码具有浮动地址,其绝对地址必须经过BL51连接定位后才能确定.为了能够在C ...

随机推荐

  1. ZOJ 3829 Known Notation 乱搞

    乱搞: 1.数字的个数要比*的个数多一个,假设数字不足须要先把数字补满 2.最优的结构应该是数字都在左边,*都在右边 3.从左往右扫一遍,遇到数字+1,遇到*-1,假设当前值<1则把这个*和最后 ...

  2. android Context的理解

    很多初入Android开发的网友向我们问到Context有什么作用,很多地方都用到它,这里Android123给这些新入门的网友做个简单的解释:  Context字面意思上下文,位于framework ...

  3. Java 自定义日志写入

    /** * 将信息写入到日志 * @param content * @return * @throws IOException */ public static boolean writeLog(St ...

  4. Delphi高仿Windows扫雷游戏(全部都是贴图绘制)

    http://www.newxing.com/Code/Delphi/game/543.html http://www.newxing.com/Code/Delphi/Network/1324.htm ...

  5. HDU4850 构造一个长度n串,它需要随机长度4子是不相同

    n<=50W.(使用26快报) 构造函数:26一个.截至构建26^4不同的字符串,最长的长度26^4+3.如此之大的输出"impossble",被判重量的四维阵列. 在正向结 ...

  6. hdu 3874 Necklace(线段树)

    这道题目和我之前做过的一道3xian大牛出的题目很像,不过总的来说还是要简单一点儿. 计算区间内的值的时候如果两个值相等,只能计算其中一个. 这道题需要将所有的问题输入之后再计算,首先,对所有问题的右 ...

  7. Android ListView 之 SimpleAdapter 二 (包含 item 中按钮监听)

    1    MainActivity.java package com.myadapter; import java.util.ArrayList; import java.util.HashMap; ...

  8. mysql-5.6.13在windows平台下的安装、使用(图解)

    本文同步至:http://www.waylau.com/mysql-5-6-13-windows-platform-installation-use-graphic/ 一. 首先电脑要具备.Net F ...

  9. 【玩转cocos2d-x之四十】怎样在Cocos2d-x 3.0中使用opengl shader?

    有小伙伴提出了这个问题.事实上GLProgramCocos2d-x引擎自带了.全然能够直接拿来用. 先上图吧. 使用opengl前后的对照: watermark/2/text/aHR0cDovL2Js ...

  10. 适用函数ALSM_EXCEL_TO_INTERNAL_TABLE把excel文件传输到内表中

    FM:ALSM_EXCEL_TO_INTERNAL_TABLE 是上载Excel文件的一个函数,但是这个函数有两个限制. 一是每个CELL只能导入前50个字符,二是如果超过9999行,行号会初始化为从 ...