- 论坛徽章:
- 0
|
怎么做快照呢?
- int my_find(const std::string& dirname,
- const std::string& fileRegular,
- std::vector<std::string>& vecFileNames)
- {
- DIR *d;
- struct dirent *drt;
- if ((d = opendir(dirname.c_str())) == NULL)
- {
- std::cout << "open dir " << dirname << "err" << std::endl;
- return 1;
- }
- while ((drt = readdir(d)) != NULL)
- {
- if ((strcmp( drt->d_name, "." ) != 0) && (strcmp( drt->d_name, ".." ) != 0))
- {
- char szTempDir[MAX_PATHH] = { 0 };
- sprintf(szTempDir, "%s/%s", dirname.c_str(), drt->d_name);
- struct stat buf;
- stat(szTempDir, &buf);
- if (S_ISDIR(buf.st_mode))
- {
- //如果是目录,递归调用
- my_find(szTempDir, fileRegular, vecFileNames);
- }
- else
- {
- std::string tmpFileName = szTempDir;
- if (tmpFileName.find(fileRegular) != std::string::npos)
- vecFileNames.push_back(szTempDir);
- }
- }
- }
- closedir(d);
- return 0;
- }
- int main(int argc, char *argv[])
- {
- if (argc != 3) return 0;
- std::string findedFiles = argv[2];
- std::vector<std::string> vecSrcFileNames;
- my_find(argv[1], findedFiles, vecSrcFileNames);
- int fileNum = static_cast<int>(vecSrcFileNames.size());
-
- std::cout << "file number : " << fileNum << std::endl;
- std::vector<std::string>::iterator it = vecSrcFileNames.begin();
- for (; it != vecSrcFileNames.end(); ++it)
- std::cout << *it << std::endl;
- return fileNum;
- }
复制代码
这是我的测试程序 单纯查某个目录下的文件没有问题 但如果该目录不断有程序进行创建删除文件的话就有时会找重复了 |
|