opendir,closedir,readdir ,telldir的·使用详解及例子

linux下查找文件夹下的目录,扫描目录。得到要查找的东西  ,linux一切皆文件说的就是这个道理,其实和文件操作差不多

1,首先加入头文件

#include <sys/types.h>

#include <dirent.h>

2,opendir

DIR *opendir(const char *name);  //参数name 文件夹的名字

如果打开成功的话返回一个dir结构的指针

如果失败的话返回一个空指针

如果文件中的文件过多也可能打卡失败

3,readdir

struct dirent *readdir(DIR *dirp);

The readdir function returns a pointer to a structure detailing the next directory entry in the directory

stream dirp. Successive calls to readdir return further directory entries. On error, and at the end of the

directory, readdir returns NULL. POSIX-compliant systems leave errno unchanged when returning

NULL at end of directory and set it when an error occurs.

函数成功的话返回一个指针,该指针的结构里保存着目录流中下一个目录项的资料,如果错误或者到了文件追后一个的话,返回NULL;

struct dirent 结构体中有两个元素

ino_t d_ino: The inode of the file//文件的inode节点

? char d_name[]: The name of the file//文件名

想要了解目录中的某个文件,需要了解stat的使用,我们下文介绍。

4,telldir

long int telldir(DIR *dirp);

函数返回文件流的第几位,后面可以用seekdir设置到第几位

void seekdir(DIR *dirp, long int loc);

5,closedir

函数关闭目录文件

int closedir(DIR *dirp);

It returns0 on success and –1 if there is an error.

返回0为成功,1失败。

6,下面为手写的printdir函数用于查看文件下面目录

#include <unistd.h>

#include <stdio.h>

#include <dirent.h>

#include <string.h>

#include <sys/stat.h>

#include <stdlib.h>

void printdir(char *dir, int depth)

{

DIR *dp;

struct dirent *entry;

struct stat statbuf;

if((dp = opendir(dir)) == NULL) {

fprintf(stderr,”cannot open directory: %s\n”, dir);       //判断dir是否是个文件夹

return;

}

chdir(dir);

while((entry = readdir(dp)) != NULL) {              //一直读取dir里面的文件直到最后一个文件。

lstat(entry->d_name,&statbuf);

if(S_ISDIR(statbuf.st_mode)) {

/* Found a directory, but ignore . and .. */

if(strcmp(“.”,entry->d_name) == 0 ||

strcmp(“..”,entry->d_name) == 0)

continue;

printf(“%*s%s/\n”,depth,”“,entry->d_name);

/* Recurse at a new indent level */

printdir(entry->d_name,depth+4);     //递归调用,文件夹下面的文件再查找

}

else printf(“%*s%s\n”,depth,”“,entry->d_name);

}

chdir(“..”);

closedir(dp);

}

int main()

{

printf(“Directory scan of /home:\n”);

printdir(“/home”,0);

printf(“done.\n”);

exit(0);

}

运行结果

$ ./printdir

Directory scan of /home:

neil/

.Xdefaults

.Xmodmap

.Xresources

.bash_history

.bashrc

.kde/

share/

apps/

konqueror/

dirtree/

public_html.desktop

toolbar/

bookmarks.xml

konq_history

kdisplay/

color-schemes/

时间: 2024-09-12 04:06:24

opendir,closedir,readdir ,telldir的·使用详解及例子的相关文章

Lambda表达式详解(例子详解)(转自:http://blog.csdn.net/damon316/article/details/51734661)

Lambda表达式详解(例子详解) lambda简介 lambda运算符:所有的lambda表达式都是用新的lambda运算符 " => ",可以叫他,"转到"或者 "成为".运算符将表达式分为两部分,左边指定输入参数,右边是lambda的主体. lambda表达式: 1.一个参数:param=>expr 2.多个参数:(param-list)=>expr 上面这些东西,记着,下面我们开始应用并阐述lambda,让你乐在其中.

Protocol Buffers编码详解,例子,图解

本文不是让你掌握protobuf的使用,而是以超级细致的例子的方式分析protobuf的编码设计.通过此文你可以了解protobuf的数据压缩能力来自什么地方,版本兼容如何做到的,其Key-Value编码的设计思路.如果你详细了解此文,你应该就能具备自己造一套编解码轮子的能力(至少基本思路). 测试的例子 阅读图片时请对比前面的例子和表格.每个字段的名称都是包含了tag的. message S2 { optional int32 s2_1 = 1; optional string s2_2 =

FreeBSD vmstat详解(附例子)

top是给Linux设计的.在FreeBSD VM里面的Free概念和其他OS完全不同,使用top查看Free内存对于FreeBSD来说可以说没什么意义.正确的方法是看vmstat. # vmstat procs memory page disk faults cpu r b w avm fre flt re pi po fr sr ad0 in sy cs us sy id 0 2 1 270512 20316 30 0 0 0 26 5 1223 1589 98 593 1 1 99 最好使

log4j.properties详解与例子

在项目中的classes 中新建立一个log4j.properties文件即可: 在实际编程时,要使Log4j真正在系统中运行事先还要对配置文件进行定义.定义步骤就是对Logger.Appender及Layout的分别使用.Log4j支持两种配置文件格式,一种是XML格式的文件,一种是java properties(key=value)[Java特性文件(键=值)].(这里只说明properties文件) 1.配置根Logger 其语法为:         log4j.rootLogger =

Windows驱动开发技术详解HelloWDM例子win7下无法安装

HelloWDM例子编译完成之后,在win7下安装显示 查看setupapi.dev看到如下信息 这个C:\MyDriver_Check目录完全不是我指定的,我放到c盘根目录下 查看inf [SourceDisksFiles] HelloWDM.sys = 1,MyDriver_Check, 把该字段修改为 HelloWDM.sys = 1 即可在win7下正确安装,这里“MyDriver_Check”指定了子目录,不过笔者试过在XP下面,没有修改inf安装的话,会弹出一个选择框让你重新选择.s

css笔记-选择器详解

css笔记-选择器详解 CSS通过选择器来定位要应用样式的元素. 下面对所有的选择器做了一个解释(CSS为版本号). CSS选择器详解 选择器 例子 例子描述 CSS .class .intro 选择 class="intro" 的所有元素. 1 #id #firstname id="firstname" 的所有元素. 1 * * 选择所有元素. 2 element p 选择所有 <p> 元素. 1 element,element div,p 选择所有

Adaboost算法详解(haar人脸检测)

Adaboost是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器(弱分类器),然后把这些弱分类器集合起来,构成一个更强的最终分类器(强分类器).Adaboost算法本身是通过改变数据分布来实现的,它根据每次训练集之中每个样本的分类是否正确,以及上次的总体分类的准确率,来确定每个样本的权值.将修改过权值的新数据集送给下层分类器进行训练,最后将每次得到的分类器最后融合起来,作为最后的决策分类器. 算法概述 1.先通过对N个训练样本的学习得到第一个弱分类器: 2.将分错的样本和其他的新数据

详解亿级大数据表的几种建立分区表的方式

自5.1开始对分区(Partition)有支持,一张表最多1024个分区 查询分区数据: SELECT * from table PARTITION(p0) 水平分区(根据列属性按行分) 举个简单例子:一个包含十年发票记录的表可以被分区为十个不同的分区,每个分区包含的是其中一年的记录. 垂直分区(按列分) 举个简单例子:一个包含了大text和BLOB列的表,这些text和BLOB列又不经常被访问,这时候就要把这些不经常使用的text和BLOB了划分到另一个分区,在保证它们数据相关性的同时还能提高

perl readdir函数详解 获取目录下文件

perl readdir函数详解 2013年12月30日  Perl基础  共 763字 字号 小 中 大  暂无评论  阅读 4,384 次 readdir函数从一个用 opendir 打开的目录句柄读取目录记录也就是文件名.用法如下: readdir DIRHANDLE 在标量环境中,readdir函数返回下一个目录记录,否则,它返回undef.在列表环境中,它返回在该目录中所有剩下的记录,如果剩下没有记录了,那么这个返回可能是一个空列表.比如: opendir(THISDIR, ".&qu