Linux C++ 中的目录操作
来源:互联网 发布:跑号器源码 编辑:程序博客网 时间:2024/05/31 18:46
Linux C++ 中的目录操作
Linux的目录操作一般流程为:打开目录-读取目录(中的文件)-关闭目录。相应的函数为opendir-readdir-closedir,其原型都在/usr/include/dirent.h中定义。
原型:
#include <dirent.h>
DIR *opendir(const char *dirname);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);
DIR是directory stream,opendir函数返回dir流类型并供读取函数readdir调用;
readdir返回dirent结构体:
struct dirent
{
#ifndef __USE_FILE_OFFSET64
__ino_t d_ino;
__off_t d_off;
#else
__ino64_t d_ino;
__off64_t d_off;
#endif
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256]; /* We must not include limits.h! */
};
d_reclen表示记录长度,d_type表示文件类型(具体见后面),d_name表示文件名;
closedir返回0表示关闭成功,-1表示失败。
dirent结构体中的d_tpye的值可以为以下枚举成员:
enum
{
DT_UNKNOWN = 0, //未知类型
# define DT_UNKNOWN DT_UNKNOWN
DT_FIFO = 1, //管道
# define DT_FIFO DT_FIFO
DT_CHR = 2, //字符设备文件
# define DT_CHR DT_CHR
DT_DIR = 4, //目录
# define DT_DIR DT_DIR
DT_BLK = 6, //块设备文件
# define DT_BLK DT_BLK
DT_REG = 8, //普通文件
# define DT_REG DT_REG
DT_LNK = 10, //连接文件
# define DT_LNK DT_LNK
DT_SOCK = 12, //套接字类型
# define DT_SOCK DT_SOCK
DT_WHT = 14 //
# define DT_WHT DT_WHT
};
示例:
#include<dirent.h>
string testPath;
DIR* pDir = NULL;
struct dirent* ent = NULL;
pDir = opendir(testPath.c_str());
if (NULL == pDir){
return false;
}
while (NULL != (ent=readdir(pDir)))
{
if(ent->d_type == 8) //普通文件
{
...
}
}
...
closedir(pDir);
pDir = NULL;
http://blog.csdn.net/zebrince/article/details/6978774
- c语言中的目录操作
- Linux C++ 中的目录操作
- Linux C++ 中的目录操作
- linux -> C/C++ 目录操作
- Linux c 目录操作函数scandir
- C语言目录操作 (Linux/Unix)
- Linux C编程--目录文件操作
- Linux c 目录操作函数scandir
- Linux c 目录操作函数scandir
- Linux c目录操作函数scandir
- Linux c 目录操作函数scandir
- linux c关于目录是否存在,新建目录等操作
- 使用linux c 目录操作函数,读取任一目录下的所有目录信息文件信息
- Linux 下C语言文件-目录基本操作
- linux c/c++ 编程之-----文件目录操作
- Linux C/C++ ----目录文件的操作(opendir,readdir,closedir)
- 20150123 【 Linux-C 文件目录操作 】 统计代码行数
- Linux下C/C++ 目录和文件操作
- 爬虫原理
- UML类图各符号含义
- 点击ImageButton时有“按下”的效果的实现
- C++高质量编程速读
- Linux下使用Zlib压缩库,进行文件的压缩与解压缩 (一)
- Linux C++ 中的目录操作
- python 编码方式总结
- 使用GDI+支持PNG显示
- 使用jquery template绑定json数据
- mysql权限管理
- DialogFragment以及AlertDialogFragment
- [Cocoa]深入浅出 Cocoa 之 Core Data(1)- 框架详解
- android 飞行模式 注册广播后 三种状态监听
- 手机已安装程序安装时间(最后一次安装)