C++ 常用小工具
1. 文件遍历
- 使用boost
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
void readfiles(string dir){
for(directory_iterator itr=directory_iterator(dir); itr!=directory_itr; ++itr){
string filePath = itr->path().string();
string fileName = itr->path().filename().string();
}
}
CMakeLists.txt 中添加
target_link_libraries(main boost_system boost_filesystem)
- 不使用boost
void TableDetector::getFileNames(std::string path, std::vector<std::string> &files){
if(path[path.size()-1]=='/'){
path = path.substr(0, path.size()-1);
}
DIR *dir;
struct dirent *ptr;
char base[1000];
if ((dir=opendir(path.c_str())) == NULL) {
perror("Open dir error...");
exit(1);
}
while ((ptr=readdir(dir)) != NULL)
{
if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) { ///current dir OR parrent dir
continue;
} else if(ptr->d_type == 8) { ///file
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
files.push_back(string(ptr->d_name));
} else if(ptr->d_type == 10) { ///link file

本文汇总了C++中的一些常用小工具,包括文件遍历(可选boost库),文件目录的操作,字符分割和特殊字符处理,获取运行目录和环境变量的方法,以及时间计数的两种方式,同时涉及含中文字符串的处理。
最低0.47元/天 解锁文章
16万+

被折叠的 条评论
为什么被折叠?



