简单的linux下的文件以及目录操作封装
2008-04-27 22:28
633 查看
#ifndef LINUXIO_H
#define LINUXIO_H
#include <vector>
#include <string>
enum FILETYPE...{ZFQ_DIR,ZFQ_FILE};
typedef struct...{
std::string name;
FILETYPE type;
}DirItem;
class LinuxIO
...{
public:
LinuxIO(void);
public:
~LinuxIO(void);
public:
static bool Dir(char* path,std::vector<DirItem>& list);
static bool MkDir(char* path);
static bool Remove(char* path);
static bool RmDir(char* path);
};
#endif
#include "LinuxIO.h"
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
LinuxIO::LinuxIO(void)
...{
}
LinuxIO::~LinuxIO(void)
...{
}
bool LinuxIO::Dir( char* path,std::vector<DirItem>& list )
...{
if(access(path,0)==-1)
return false;
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
while ((ent=readdir(pDir))!=NULL)
...{
if( strcmp(ent->d_name, ".")==0 || strcmp(ent->d_name, "..")==0 )
...{
continue;
}
if (ent->d_type==8)
...{
DirItem temp;
temp.name=(std::string)ent->d_name;
temp.type=ZFQ_FILE;
list.push_back(temp);
}
else
...{
DirItem temp;
temp.name=(std::string)ent->d_name;
temp.type=ZFQ_DIR;
list.push_back(temp);
}
}
return true;
}
bool LinuxIO::MkDir( char* path )
...{
if(mkdir(path,S_IRWXU)==0)
return true;
else
return false;
}
bool LinuxIO::Remove( char* path )
...{
if(remove(path)==0)
return true;
else
return false;
}
bool LinuxIO::RmDir( char* path )
...{
if(access(path,0)==-1)
return false;
std::vector<DirItem> list;
Dir(path,list);
std::vector<DirItem> fullnamelist;
char namebuffer[PATH_MAX];
strcpy(namebuffer,path);
strcat(namebuffer,"/");
int i;
for(i=0;i<(int)list.size();i++)
...{
char tempbuffer[PATH_MAX];
strcpy(tempbuffer,namebuffer);
strcat(tempbuffer,list[i].name.c_str());
if(list[i].type==ZFQ_FILE)
Remove(tempbuffer);
else
RmDir(tempbuffer);
}
rmdir(path);
return true;
}
相关文章推荐
- Android与LinuxC目录创建以及文件读写操作
- Linux下目录文件的操作(opendir,readdir,closedir) 以及DIR,dirent,stat等结构体详解
- Linux中对文件以及目录的基础操作
- Linux 给用户及用户组分配权限以及对文件目录的操作
- linux 简单指令以及文件操作
- Linux 给用户及用户组分配权限以及对文件目录的操作
- Linux学习笔记:linux下的文件以及对文件的简单操作
- linux中C的Makefile多个文件目录,以及VPATH的简单示例
- linux文件和目录操作命令
- 【Linux系列教程】02.命令基本格式、常见目录以及文件目录处理命令
- Linux学习记录--文件系统简单操作
- linux下批量修改目录以及子目录里的所有文件的时间
- Linux 文件的管理与目录之 ls 、 cp 命令操作详解
- linux编程学习--常见文件目录操作
- Linux运维笔记-课后作业-正则表达式和简单文件操作
- Linux中在主机上实现对备机上目录及文件的操作的C代码实现
- linux进入一个目录以及在目录中创建文件所需要的权限
- 详谈Linux文件系统基础和相关操作以及swap分区
- Linux学习一——文件和目录的基本操作
- linux文件和目录操作(附ls命令实现)