使用boost库获取文件名列表

#include <string>

#include <vector>

#include <iterator>

#include <iostream>

#include <fstream>

#include <boost/algorithm/string.hpp>

#include <boost/algorithm/string/regex.hpp>

#include <boost/regex.hpp>

#include <boost/filesystem.hpp>

using namespace std;

 

typedef std::vector < std::string > stringVector;

 

void getFileList(const string& pathName, stringVector &recusiveFileVec)

{

boost::filesystem::recursive_directory_iterator rdi(pathName);

boost::filesystem::recursive_directory_iterator end_rdi;

recusiveFileVec.empty();

 

for (; rdi != end_rdi; rdi++) {

if (is_directory(*rdi)) {

std::cout << *rdi << "is pathName" << std::endl;

} else {

recusiveFileVec.push_back(rdi->path().string());

std::cout << *rdi << " is a file" << std::endl;

}

}

 

int  processFile( const string &filePathName, const  char* directory)

{

boost::regex pathRegex(directory);

    std::string fmt("");

std::string path = regex_replace(filePathName, pathRegex, fmt);  

stringVector filePathVec;

boost::split(filePathVec, path, boost::is_any_of("/"));

int size = filePathVec.size();

string category;

if (size > 2) {

category = "__" + filePathVec[size - 3] + "__" + filePathVec[size - 2];

} else if (size == 2) {

category = string("__") + filePathVec[size - 2];

} else {

category = "";

}

string tempFileName = filePathName + string(".bak");

std::ifstream inputFile(filePathName.c_str());

std::ofstream outfile(tempFileName.c_str());

 

if (inputFile.is_open()) {

while (inputFile.good()) {

string line;

getline(inputFile, line);

stringVector lineVec;

boost::split(lineVec, line, boost::is_any_of("\t"));

if (lineVec.size() < 2) {  

//cout << "This  line only has one segment by  '\t'";

continue;

}

else {

stringVector titleVec;

boost::algorithm::split_regex( titleVec, lineVec[0], boost::regex( " [-|&] " )) ;

string outString;

size = titleVec.size();

cout << "size = " << size << endl;

if (size == 1) {

outString = titleVec[0] + lineVec[1] + category;

cout << "outString = " << outString << endl;

outfile << outString << endl;

}

else if (size == 2) {

outString =  titleVec[0] + string("##")  + lineVec[1] + line + category;

cout << "outString = " << outString << endl;

outfile << outString << endl;

outString =  titleVec[1] + string("##")  + lineVec[1] + line + category;

cout << "outString = " << outString << endl;

outfile << outString << endl;

}

}

}

} else {

cout << "Unable to open file";

return -1;

}

inputFile.close();

outfile.close();

// boost::filesystem::remove(filePath.c_str());

// boost::filesystem::rename(tempfile.c_str(), filePath.c_str());

return 1;

}

 

 

int main(int argc, char *argv[])

{

if (argc < 2) {

cout << "Invalid arg --- You should input a Directory path as an argument.(ex: /home/hello" << endl;

return -1;

}

stringVector fileList;

getFileList(argv[1], fileList);

for (int i = 0; i < fileList.size(); i++) {

processFile(fileList[i], argv[1]);

cout<<fileList[i]<<endl;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值