static bool getFileNamesInDir(const string strDir, vector<string> &vecFileName)
{
DIR* dir = NULL;
struct dirent entry;
struct dirent* entryPtr = NULL;
char realPath[1024];
realpath(strDir.c_str(), realPath);
string strRealPath = realPath;
dir = opendir(realPath);
if (NULL == dir) {
cout << strerror(errno) << ", strDir : " << strDir << endl;
return false;
}
readdir_r(dir, &entry, &entryPtr);
while (entryPtr != NULL) {
if (entry.d_type == DT_REG) {
string strFileName = entry.d_name;
if ("." == strFileName || ".." == strFileName) {
}
else {
if (getFileLength(strRealPath + "/" + strFileName) == blockSize) {
vecFileName.push_back(strRealPath + "/" + strFileName);
}
}
}
else if(entry.d_type == DT_DIR) {
string dir = entry.d_name;
if (!("." == dir || ".." == dir)) {
getFileNamesInDir(strRealPath + "/" + dir, vecFileName);
}
}
readdir_r(dir, &entry, &entryPtr);
}
return true;
}
这个函数的意思是读取string strDir目录下的文件,存到vecFileName这个vector里面。其中忽略.和..两个文件。
然后遍历这个vector,对每个元素执行打开文件读取操作就好了。