Linux 目录操作 fdopendir closedir readdir

//@@只有操作系统内核才有权限对目录进行写操作
#include <sys/types.h>
#include <dirent.h>


//打开目录
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
RETURN VALUE
The opendir() and fdopendir() functions return a pointer to  the  directory  stream.
 On error, NULL is returned, and errno is set appropriately.


DIR是个内部的结构,函数用来保存正在被读的目录信息

//关闭目录
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
returns 0 on success.  On error, -1 is returned
//目录读
struct dirent  *readdir(DIR *dp);
struct dirent {
               long  d_ino;              /* inode number */
               char  d_name[NAME_MAX+1]; /* filename (null-terminated) */
  }


参考程序:


//目录操作  打开目录输出目录中的文件 inode  名称
//code by JZ
#include<unistd.h>
#include<iostream>
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
#include<dirent.h>
#include<limits.h>
#include<sys/stat.h>
using namespace std;
int main(int argc,char *argv[])
{

	DIR *dp;
	struct dirent* dirp;
	if(   ( dp=opendir(argv[1]) )==NULL  )
		return -1;
	while( ( dirp=readdir(dp) )!=NULL )
	{
		cout<<"i-node:"<<dirp->d_ino<<"  name:"<<dirp->d_name<<endl;
	}
	closedir(dp);
	return 0;
}




/*
[jz@localhost ~]$  g++ main.cpp 
[jz@localhost ~]$ ./a.out  /home/jz/

//结果:
i-node:789463  name:info
i-node:789671  name:.ICEauthority
i-node:791731  name:.bashrc
i-node:792177  name:main.cpp
i-node:792187  name:a.out
i-node:789459  name:man
i-node:792178  name:result
i-node:789704  name:.esd_auth
i-node:789648  name:Downloads
i-node:789683  name:.fontconfig
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值