linux 目录操作函数opendir readdir closedir,文件stat
2012-02-16 13:39
716 查看
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *p a t h n a m e ) ;
返回:若成功则为指针,若出错则为N U L L
struct dirent *readdir(DIR *d p ) ;
返回:若成功则为指针,若在目录尾或出错则为N ULL
int closedir(DIR *d p ) ;
返回:若成功则为0,若出错则为-1
D I R结构是一个内部结构,它由这四个函数用来保存正被读的目录的有关信息。其作用类
似于F I L E结构。由o p e n d i r返回的指向D I R结构的指针由另外三个函数使用。o p e n d i r执行初始化操作,使第
一个r e a d d i r读目录中的第一个目录项。目录中各目录项的顺序与实现有关。它们通常并不按字
母顺序排列。
程序:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
using namespace std;
int main(int argc ,char **argv)
{
DIR *pDir = NULL;
struct dirent *pDirent = NULL;
if((pDir = opendir("./")) == NULL) //读取目录
{
switch (errno)
{
case EACCES:
cout << "Permission denied." << endl;
break;
case EMFILE:
cout << " Too many file descriptors in use by process." << endl;
break;
case ENFILE:
cout << " Too many files are currently open in the system." << endl;
break;
case ENOENT:
cout << " Directory does not exist, or name is an empty string." << endl;
break;
case ENOMEM:
cout << " Insufficient memory to complete the operation." << endl;
break;
case ENOTDIR:
cout << " name is not a directory." << endl;
break;
default:
cout << "unknow error" << endl;
}
return -1;
}
int i = 0;
while(true)
{
i++;
pDirent = readdir(pDir) ; //读取单一文件
if(pDirent == NULL)
{
cout<<i<<"====pdirent is null"<<endl;
break;
}
if ((strcmp((const char *)pDirent->d_name, ".") == 0) ||(strcmp((const char *)pDirent->d_name, "..") == 0))
{
continue;
}
printf("file name:%s\n",pDirent->d_name);
char cNewDir[1024];
memset(cNewDir, 0, sizeof(cNewDir));
sprintf(cNewDir, "%s/%s", "./", (char *)pDirent->d_name);
struct stat mstat;
memset(&mstat,0,sizeof(mstat));
if(lstat(cNewDir,&mstat) == -1) //获取文件属性结构 ,此处需要使用lstat,如使用stat,则不能判断链接文件的信息
{
perror("stat:");
continue;
}
if(S_ISDIR(mstat.st_mode)) //判断是否是目录
{
printf("file:%s is a directory\n",pDirent->d_name);
}
else if(S_ISREG(mstat.st_mode))
{
printf("file:%s is a regular file.\n",pDirent->d_name);
}
else if(S_ISCHR(mstat.st_mode))
{
printf("file:%s is a character special file.\n",pDirent->d_name);
}
else if(S_ISBLK(mstat.st_mode))
{
printf("file:%s is a block file.\n",pDirent->d_name);
}
else if(S_ISFIFO(mstat.st_mode))
{
printf("file:%s is a fifo file.\n",pDirent->d_name);
}
else if(S_ISLNK(mstat.st_mode))
{
printf("file:%s is a symbolic link file.\n",pDirent->d_name);
}
else if(S_ISSOCK(mstat.st_mode))
{
printf("file:%s is a socket file.\n",pDirent->d_name);
}
else
{
printf("file:%s is an unknown type file.\n",pDirent->d_name);
}
}
closedir(pDir);
return 0;
}
#include <dirent.h>
DIR *opendir(const char *p a t h n a m e ) ;
返回:若成功则为指针,若出错则为N U L L
struct dirent *readdir(DIR *d p ) ;
返回:若成功则为指针,若在目录尾或出错则为N ULL
int closedir(DIR *d p ) ;
返回:若成功则为0,若出错则为-1
D I R结构是一个内部结构,它由这四个函数用来保存正被读的目录的有关信息。其作用类
似于F I L E结构。由o p e n d i r返回的指向D I R结构的指针由另外三个函数使用。o p e n d i r执行初始化操作,使第
一个r e a d d i r读目录中的第一个目录项。目录中各目录项的顺序与实现有关。它们通常并不按字
母顺序排列。
程序:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
using namespace std;
int main(int argc ,char **argv)
{
DIR *pDir = NULL;
struct dirent *pDirent = NULL;
if((pDir = opendir("./")) == NULL) //读取目录
{
switch (errno)
{
case EACCES:
cout << "Permission denied." << endl;
break;
case EMFILE:
cout << " Too many file descriptors in use by process." << endl;
break;
case ENFILE:
cout << " Too many files are currently open in the system." << endl;
break;
case ENOENT:
cout << " Directory does not exist, or name is an empty string." << endl;
break;
case ENOMEM:
cout << " Insufficient memory to complete the operation." << endl;
break;
case ENOTDIR:
cout << " name is not a directory." << endl;
break;
default:
cout << "unknow error" << endl;
}
return -1;
}
int i = 0;
while(true)
{
i++;
pDirent = readdir(pDir) ; //读取单一文件
if(pDirent == NULL)
{
cout<<i<<"====pdirent is null"<<endl;
break;
}
if ((strcmp((const char *)pDirent->d_name, ".") == 0) ||(strcmp((const char *)pDirent->d_name, "..") == 0))
{
continue;
}
printf("file name:%s\n",pDirent->d_name);
char cNewDir[1024];
memset(cNewDir, 0, sizeof(cNewDir));
sprintf(cNewDir, "%s/%s", "./", (char *)pDirent->d_name);
struct stat mstat;
memset(&mstat,0,sizeof(mstat));
if(lstat(cNewDir,&mstat) == -1) //获取文件属性结构 ,此处需要使用lstat,如使用stat,则不能判断链接文件的信息
{
perror("stat:");
continue;
}
if(S_ISDIR(mstat.st_mode)) //判断是否是目录
{
printf("file:%s is a directory\n",pDirent->d_name);
}
else if(S_ISREG(mstat.st_mode))
{
printf("file:%s is a regular file.\n",pDirent->d_name);
}
else if(S_ISCHR(mstat.st_mode))
{
printf("file:%s is a character special file.\n",pDirent->d_name);
}
else if(S_ISBLK(mstat.st_mode))
{
printf("file:%s is a block file.\n",pDirent->d_name);
}
else if(S_ISFIFO(mstat.st_mode))
{
printf("file:%s is a fifo file.\n",pDirent->d_name);
}
else if(S_ISLNK(mstat.st_mode))
{
printf("file:%s is a symbolic link file.\n",pDirent->d_name);
}
else if(S_ISSOCK(mstat.st_mode))
{
printf("file:%s is a socket file.\n",pDirent->d_name);
}
else
{
printf("file:%s is an unknown type file.\n",pDirent->d_name);
}
}
closedir(pDir);
return 0;
}
相关文章推荐
- Linux 文件操作函数 opendir()、readdir()、closedir()、stat()
- Linux下目录文件的操作(opendir,readdir,closedir) 以及DIR,dirent,stat等结构体详解
- linux目录相关函数opendir、readdir、rewinddir、closedir、telldir、seekdir、stat函数和DIR、dirent、stat结构体详解
- Linux C/C++ ----目录文件的操作(opendir,readdir,closedir)
- 目录操作函数opendir、readdir和closedir
- 文件操作opendir()/readdir()/closedir()/stat()
- 文件操作opendir()/readdir()/closedir()/stat()
- linux c下目录文件的操作(opendir,readdir,closedir)
- opendir、readdir和closedir(PHP操作目录和文件)
- 文件操作opendir()/readdir()/closedir()/stat() .
- linux c学习笔记----目录文件的操作(opendir,readdir,closedir)
- php读取目录文件函数is_dir(),opendir(),readdir(),closedir()
- 解析linux 文件和目录操作的相关函数
- unix文件和目录操作的函数stat()、fstat()、lstat()、access()、umask()函数
- linux文件和目录的操作函数
- Linux 文件和目录操作的相关函数
- 使用linux c 目录操作函数,读取任一目录下的所有目录信息文件信息
- Linux文件系统文件属性及目录操作函数
- perl中目录句柄的操作(opendir, readdir, closedir)
- linux xfs文件系统无法用readdir获取dirent文件类型d_type则用stat获取暨stat函数讲解