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 <iostream>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <cstdio>
using namespace std;
bool IsFolderExist(const char *folder);
bool IsFileExist(const char* file);
void listDirFile(char *dirfile);
int main(int argc, char *argv[])
{
char folderbuf[50];
memset(folderbuf, 0, 50);
getcwd(folderbuf, 50);
listDirFile(folderbuf);
bool res = IsFolderExist(folderbuf);
if(res)
cout << "Folder OK\n";
else
cout << "Folder error\n";
int len = strlen(folderbuf);
folderbuf[len] = '/';
strcat(folderbuf, "test_folder.cpp");
res = IsFileExist(folderbuf);
if(res)
cout << "File ok\n";
else
cout << "File error\n"; return 0;
}
bool IsFolderExist(const char *folder)
{
DIR *dir;
if(folder == NULL)
{
return false;
}
if((dir = opendir(folder)) == NULL)
{
return false;
}
closedir(dir);
return true;
}
bool IsFileExist(const char* file)
{
if(file == NULL)
{
return false;
}
if(access(file, F_OK) == 0)
return true;
return false;
}
void listDirFile(char *dirfile)
{
char childpath[200];
memset(childpath,0, sizeof(childpath));
DIR *dp;
struct dirent *ent;
dp = opendir(dirfile);
while(NULL !=(ent=readdir(dp)))
{
if(ent->d_type & DT_DIR)
{
if(strcmp(ent->d_name, ".")==0 || strcmp(ent->d_name, "..")==0)
continue;
sprintf(childpath, "%s/%s", dirfile, ent->d_name);
cout << childpath <<endl;
listDirFile(childpath);
}
else{}
}
}
实例二、
#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
using namespace std;
int isdirectory(char* direcpath);
int lib_directory_list(char* dirpath)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dirpath)) == NULL)
{
fprintf(stderr, "can't open the directory");
return -1;
}
while((dirp = readdir(dp)) != NULL)
{
printf("%s\n", dirp->d_name);
}
closedir(dp);
printf("\n");
return 0;
}
int lib_file_list(char* dirpath)
{
DIR *dp;
struct dirent *dirp;
char filename[300];
memset(filename,0, sizeof(filename));
if((dp = opendir(dirpath)) == NULL)
{
return -1;
}
while((dirp = readdir(dp)) != NULL)
{
if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name, "..")==0)
continue;
if(isdirectory(dirp->d_name))
{
sprintf(filename, "%s/%s", dirpath, dirp->d_name);
lib_file_list(filename);
}
printf("%s\n", filename);
}
closedir(dp);
printf("\n");
return 0;
}
int isdirectory(char* direcpath)
{
struct stat sDir;
if(stat(direcpath, &sDir) < 0)
return -1;
if(S_IFDIR == (sDir.st_mode & S_IFMT))
return 1;
return -1;
}
int main(int argc, char *argv[])
{
char* directory = "/home/laijia";
lib_directory_list(directory);
lib_file_list(directory);
return 0;
}
创建文件目录:
#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
using namespace std;
int isdirectory(char* direcpath);
int lib_directory_list(char* dirpath)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dirpath)) == NULL)
{
fprintf(stderr, "can't open the directory");
return -1;
}
while((dirp = readdir(dp)) != NULL)
{
printf("%s\n", dirp->d_name);
}
closedir(dp);
printf("\n");
return 0;
}
int lib_file_list(char* dirpath)
{
DIR *dp;
struct dirent *dirp;
char filename[300];
memset(filename,0, sizeof(filename));
if((dp = opendir(dirpath)) == NULL)
{
return -1;
}
while((dirp = readdir(dp)) != NULL)
{
if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name, "..")==0)
continue;
if(isdirectory(dirp->d_name))
{
sprintf(filename, "%s/%s", dirpath, dirp->d_name);
lib_file_list(filename);
}
printf("%s\n", filename);
}
closedir(dp);
printf("\n");
return 0;
}
int isdirectory(char* direcpath)
{
struct stat sDir;
if(stat(direcpath, &sDir) < 0)
return -1;
if(S_IFDIR == (sDir.st_mode & S_IFMT))
return 1;
return -1;
}
int main(int argc, char *argv[])
{
char* directory = "/home/laijia";
lib_directory_list(directory);
lib_file_list(directory);
return 0;
}
bool createMkdir(std::string& dirpath)
{
mode_t umask_value = umask (0);
umask (umask_value);
mode_t mode = ((S_IRWXU | S_IRWXG | S_IRWXO) & (~ umask_value)) | S_IWUSR | S_IXUSR;
std::string path;
int index;
int pos = dirpath.find("/");
path = dirpath.substr(0, ++pos);
std::cout << path << "\n";
while(!isDir(path))
{
if(!mkdir(path.c_str(), mode))
return false;
index = dirpath.find("/", pos);
if(index == std::string::npos)
{
break;
}
path += dirpath.substr(pos, index-pos+1);
//std::cout << path << "\n";
pos = ++index;
}
return true;
}
bool isDir(std::string& dirpath)
{
struct stat stats;
if(lstat(dirpath.c_str(), &stats) && S_ISDIR(stats.st_mode))
return true;
else
return false;
}
bool createMkdir(std::string& dirpath)
{
mode_t umask_value = umask (0);
umask (umask_value);
mode_t mode = ((S_IRWXU | S_IRWXG | S_IRWXO) & (~ umask_value)) | S_IWUSR | S_IXUSR;
std::string path;
int index;
int pos = dirpath.find("/");
path = dirpath.substr(0, ++pos);
std::cout << path << "\n";
while(!isDir(path))
{
if(!mkdir(path.c_str(), mode))
return false;
index = dirpath.find("/", pos);
if(index == std::string::npos)
{
break;
}
path += dirpath.substr(pos, index-pos+1);
//std::cout << path << "\n";
pos = ++index;
}
return true;
}
bool isDir(std::string& dirpath)
{
struct stat stats;
if(lstat(dirpath.c_str(), &stats) && S_ISDIR(stats.st_mode))
return true;
else
return false;
}