最近需要写个程序,处理一写日志。然后要用到遍历Linux下得所有文件和子目录这以招。看了很多,大同小异。写个自己得。
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
using namespace std;
vector<string> getfiles(string strCurrentDir){
vector<string> vFiles; // 存放文件名们
DIR *dir;
struct dirent *pDir;
if((dir = opendir(strCurrentDir.c_str())) == NULL){
cout << "open dir Faild" << endl;
exit(1);
}
while((pDir = readdir(dir)) != NULL){
if(strcmp(pDir->d_name,".")==0 || strcmp(pDir->d_name,"..")==0){
continue;
}else if(pDir->d_type == 8){ // 文件
vFiles.push_back(strCurrentDir + "/" + pDir->d_name);
}else if(pDir->d_type == 10){
continue;
}else if(pDir->d_type == 4){ // 子目录
string strNextdir = strCurrentDir + "/" + pDir->d_name;
vector<string> ss = getfiles(strNextdir);
vFiles.insert(vFiles.end(),ss.begin(),ss.end()); // 组合到一起。
}
}
closedir(dir);
// 排下序 - 根据自己喜好
sort(vFiles.begin(), vFiles.end());
return vFiles;
}
int main(){
char basePath[100]; // 当前目录
memset(basePath, '\0', sizeof(basePath));
getcwd(basePath, 999);
printf("主目录: %s\n",basePath);
vector<string> files=getfiles(basePath);
for (size_t i=0; i<files.size(); i++)
{
cout<<files[i]<<endl;
}
return 0;
}
执行得结果:[root@localhost testCode]# ./a.out
主目录: /root/xue/testCode
/root/xue/testCode/a.out
/root/xue/testCode/app
/root/xue/testCode/getdirandfile.cpp
/root/xue/testCode/testRedisSentinel.cpp
/root/xue/testCode/testRedissingle.cpp
/root/xue/testCode/testcluster.cpp
/root/xue/testCode/testdir1/shuoming.txt
/root/xue/testCode/testdir2/shuoming2.txt
我感觉这个代码有缺陷。直到我有一次读取5000个文件的文件夹,只读取了1700多个文件。
然后我跟代码发现,很多文件的type没有被认出来,都是0.
pDir->d_type == DT_UNKNOWN 。
后来经过我的排查,与翻阅资料发现。
linux c 简单的ls程序编写 学习笔记_violet_xrym的博客-CSDN博客_dt_blk
关于Linux下的stat()函数_rain_xyw的博客-CSDN博客_linux stat函数
DT_UNKNOWN
The type is unknown. Only some filesystems have full support to return the type of the file, others might always return this value.
类型未知。少数文件系统会出现此函数不支持的文件类型,另一些则总是返回这个值。译者注:总之这个值是为了应对不兼容的文件系统而设置的。
部分linux文件系统,例如xfs不支持d_type,当你用d_type时,所有文件/目录的d_type会一直为空,即0(UNKNOWN),无法判断文件类型。所以需要sta() 来显示的查一下它的文件类型。
所以,判断文件类型那里应该尽量用stat()函数来判断。
下面增加了stat()的使用,我这里偷懒了。完整的请参考我上面的第二个连接里的内容。
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include <sys/stat.h> // stat 函数所在的文件
using namespace std;
#define linux
vector<string> getFiles(string cate_dir)
{
vector<string> files;//存放文件名
#ifdef WIN32
_finddata_t file;
long lf;
//输入文件夹路径
if ((lf=_findfirst(cate_dir.c_str(), &file)) == -1) {
cout<<cate_dir<<" not found!!!"<<endl;
} else {
while(_findnext(lf, &file) == 0) {
//输出文件名
//cout<<file.name<<endl;
if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)
continue;
files.push_back(file.name);
}
}
_findclose(lf);
#endif
#ifdef linux
DIR *dir;
struct dirent *ptr;
struct stat st;
//char base[1000];
if ((dir=opendir(cate_dir.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(cate_dir + "/" + ptr->d_name);
else if(ptr->d_type == 10) ///link file
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
continue;
else if(ptr->d_type == 4) ///dir
{
//files.push_back(ptr->d_name);
string strNextdir = cate_dir + "/" + ptr->d_name;
vector<string> ss = getFiles(strNextdir);
/*
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_nSame);
readFileList(base);
*/
files.insert(files.end(),ss.begin(),ss.end()); // 组合到一起。
}else
{
// 当有的时候linux 不认识的文件类型是,需要手动调用stat来查看是否是文件类型。
// 其实应该所有的判断都用stat来,我这里只不过是懒得弄了。
string AbsolutePath = cate_dir + "/" + ptr->d_name;
if(stat(AbsolutePath.c_str(),&st)!=-1) // 读取成功
{
if((st.st_mode&S_IFMT)==S_IFREG) //普通文件
{
files.push_back(cate_dir + "/" + ptr->d_name);
}
}
else{
// 以前有很多他妈的文件类型是0 的。这些得用stat显示查看。
printf("name : %s , type :%d \n", ptr->d_name, ptr->d_type);
}
}
}
closedir(dir);
#endif
//排序,按从小到大排序
sort(files.begin(), files.end());
return files;
}
int main(){
char basePath[100]; // 当前目录
memset(basePath, '\0', sizeof(basePath));
getcwd(basePath, 999);
printf("主目录: %s\n",basePath);
vector<string> files=getfiles(basePath);
for (size_t i=0; i<files.size(); i++)
{
cout<<files[i]<<endl;
}
return 0;
}