////////lihaoyxj@gmail.com
在具体使用c++或c对于不同平台的操作时,文件操作存在很大的困难,而之前转载的ibm开发论坛上的有关boost的文件系统的介绍,也的确对于此问题有个解决方案吧,所以试着写一些针对平常的一些操作而做的类.
头文件
#ifndef __DIR_OPR_H
#define __DIR_OPR_H
#include
#include
using namespace std;
typedef struct RootDirs
{
char szName[255];
unsigned long lCapacity;
unsigned long lAvailable;//可用空间
RootDirs()
{
memset(this,0,sizeof(this));
}
};
typedef struct DirList
{
char szName[255];
int iStatus;//0,dir;1,file;2,other
unsigned long lFileLength;
char szCreateTime[TIMESTRLENGTH];
char szUpdateTime[TIMESTRLENGTH];
DirList()
{
memset(this,0,sizeof(this));
}
};
class CDirOpr
{
public:
CDirOpr();
~CDirOpr();
bool getRootDir(list& dirs);//根路径和别的路径还是不同的
bool getPath(string& path,list& dirs);//一个路径下的文件及文件夹
bool createDir(string& path); //创建目录
bool renameDir(string& path,string& newpath);//目录改名
bool deleteDir(string& path); //删除目录
bool isExist(string& path); //目录是否存在
unsigned long file_count ;
unsigned long dir_count ;
unsigned long other_count ;
unsigned long err_count;
};
#endif
实现文件
#include "../include/diropr.h"
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/progress.hpp"
#include "boost/interprocess/sync/file_lock.hpp"
#include
#include
#include
#include
namespace fs = boost::filesystem;
CDirOpr::CDirOpr()
{
file_count = 0;
dir_count = 0;
other_count = 0;
err_count = 0;
}
CDirOpr::~CDirOpr()
{
}
bool CDirOpr::getRootDir(list& dirs)
{
#ifdef _WIN32
std::string path(" :");
//std::cout << "Windows:\n盘符\t" << "总空间\t\t" << "可用空间\t" << std::endl;
for (char ch='a'; ch<='z'; ++ch){
path[0] = ch;
fs::path p(path);
try {
if (!fs::exists(p)) continue;
RootDirs rootDirs;
strcpy(rootDirs.szName,path.c_str());
//std::cout << path << '\t';
fs::space_info si = fs::space(p);
rootDirs.lCapacity=si.capacity;
rootDirs.lAvailable=si.available;
// std::cout<< si.capacity * 1.0 / GB << "GB\t"
// << si.available * 1.0 / GB << "GB\t"
// << std::endl;
dirs.push_back(rootDirs);
}
catch (const std::exception& e) {
std::cout << e.what()<< std::endl;
}
}
#else
//linux,unix
std::string path("/");
fs::path p(path);
if ( !fs::exists( p ) )
{
return 1;//没有发现该路径
}
if ( fs::is_directory( p ) )
{
std::cout << "\nIn directory: "
<< p.directory_string() << "\n\n";
fs::directory_iterator end_iter;
for ( fs::directory_iterator dir_itr( p );dir_itr != end_iter;++dir_itr )
{
try
{
if ( fs::is_directory( dir_itr->status() ) )
{
++dir_count;
RootDirs rootDirs;
//memset((void*)rootDirs,0,sizeof(rootDirs));
strcpy(rootDirs.szName,path.c_str());
//std::cout << path << '\t';
fs::space_info si = fs::space(p);
rootDirs.lCapacity=si.capacity;
rootDirs.lAvailable=si.available;
dirs.push_back(rootDirs);
//std::cout << dir_itr->path().filename() << " [directory]\n";
}
}
catch ( const std::exception & ex )
{
std::cout << dir_itr->path().filename() << " " << ex.what() << std::endl;
}
}
}
else // must be a file
{
std::cout << "\nFound: " << p.file_string() << "\n";
}
#endif
return true;
}
bool CDirOpr::getPath(string& path,list& dirs)
{
boost::progress_timer t( std::clog );
fs::path full_path( path);
if ( !fs::exists( full_path ) )
{
//std::cout << "\nNot found: " << full_path.file_string() << std::endl;
return false;
}
if ( fs::is_directory( full_path ) )
{
//std::cout << "\nIn directory: " << full_path.directory_string() << "\n\n";
fs::directory_iterator end_iter;
for ( fs::directory_iterator dir_itr( full_path ); dir_itr != end_iter;++dir_itr )
{
try
{
DirList dirList;
strcpy(dirList.szName,dir_itr->path().filename().c_str());
if ( fs::is_directory( dir_itr->status() ) )
{
++dir_count;
dirList.iStatus=0;
}
else if ( fs::is_regular_file( dir_itr->status() ) )
{
++file_count;
dirList.iStatus=1;
dirList.lFileLength = fs::file_size(dir_itr->path());
}
else
{
++other_count;
dirList.iStatus=2;
}
fs::last_write_time(dir_itr->path());
dirs.push_back(dirList);
}
catch ( const std::exception & ex )
{
++err_count;
std::cout << dir_itr->path().filename() << " " << ex.what() << std::endl;
}
}
}
else // must be a file
{
//std::cout << "\nFound: " << full_path.file_string() << "\n";
return false;
}
return true;
}
bool CDirOpr::createDir(string& path)
{
try
{
boost::filesystem::path fph(path);
return fs::create_directory(fph); // 0错误发生,否则返回删除的文件数
}
catch(...)
{
return false;
}
return true;
}
bool CDirOpr::renameDir(string& path,string& newpath)
{
try
{
boost::filesystem::path fph(path);
boost::filesystem::path newfph(newpath);
{
boost::filesystem::rename(fph,newpath); // 0错误发生,否则返回删除的文件数
}
}
catch(...)
{
return false;
}
return true;
}
bool CDirOpr::deleteDir(string& path)
{
try
{
boost::filesystem::path fph(path);
if(boost::filesystem::remove_all(fph) == 0) // 0错误发生,否则返回删除的文件数
{
return false;
}
else
{
std::cout << "File deleted successfully" << std::endl;
}
}
catch(...)
{
return false;
}
return true;
}
以上这些都比较简单,但是在根路径的处理上有时稍有点摸不着边。采用这样的目录操作,对于跨平台文件操作就有很多的可移植性,同时代码主要集中到业务代码上。我的需求是最终通过boost做成一套跨平台文件传输的模块
阅读(2008) | 评论(0) | 转发(0) |