C++读取文件夹中特定后缀的文件—dirent—vector
[root@localhost 桌面]# g++ temp.cpp -o temp.exe
[root@localhost 桌面]# ./temp.exe
/etc/nginx/html/database/processTemplate/
a
a
2
HeatingRepair.xml
TestNotePad.xml
[root@localhost 桌面]#
temp.cpp
阅读(135) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
[root@localhost 桌面]# ./temp.exe
/etc/nginx/html/database/processTemplate/
a
a
2
HeatingRepair.xml
TestNotePad.xml
[root@localhost 桌面]#
temp.cpp
点击(此处)折叠或打开
-
//indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *.c *.h *.cpp *.hpp
-
//indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *
-
-
#include <string.h>
-
#include <iostream>
-
#include <vector>
-
#include <string>
-
#include <dirent.h>
-
using namespace std;
-
-
typedef vector < string > FilesList;
-
void ls(const string & sDir, FilesList & files, const char *acExtension);
-
-
int main()
-
{
-
FilesList files;
-
ls("/etc/nginx/html/database/processTemplate/", files, ".xml");
-
-
cout << files.size() << endl;
-
-
for (int i = 0; i < files.size(); i++) //ztg add
-
{
-
cout << files[i] << endl;
-
}
-
return 0;
-
}
-
-
void ls(const string & sDir, FilesList & files, const char *acExtension)
-
{
-
int return_code;
-
DIR *dir;
-
struct dirent entry;
-
struct dirent *res;
-
-
char *sourceDir = new char[sDir.size() + 4];
-
strcpy(sourceDir, sDir.c_str());
-
//strcat(sourceDir, "\\*");
-
-
cout << sourceDir << endl;
-
-
if ((dir = opendir(sourceDir)) != NULL) { //open dir
-
for (return_code = readdir_r(dir, &entry, &res); res != NULL && return_code == 0; return_code = readdir_r(dir, &entry, &res)) {
-
if (entry.d_type != DT_DIR) { //save to files
-
//doc_list.push_back(string(entry.d_name));
-
cout << "a" << endl;
-
if ((acExtension != NULL)) {
-
-
string sFile = entry.d_name;
-
-
int iPos = sFile.find_last_of(".");
-
if ((iPos == string::npos) || (strcmp(acExtension, sFile.substr(iPos).c_str())))
-
continue;
-
}
-
-
files.push_back(entry.d_name);
-
}
-
}
-
closedir(dir); //close dir
-
}
- }
相关热门文章
给主人留下些什么吧!~~
评论热议