C++遍历指定目录下的文件代码
#include <string>
#include <vector>
#include <iostream>
#include <direct.h>
#include <io.h>
using namespace std;
void f_sample_rand(string& s_images_path);
int main()
{
string s_images_path="..//roi";
f_sample_rand(s_images_path);
system("pause");
return 0;
}
void f_sample_rand(string& s_images_path)
{
long hFile = 0;
struct _finddata_t fileinfo;
string p;
int i_counts=0;
if((hFile = _findfirst(p.assign(s_images_path).append("\\*").c_str(),&fileinfo)) != -1)
{
do
{
if(i_counts==0||i_counts==1)
{
i_counts++;
continue;
}
else
{
cout<<fileinfo.name<<endl;
i_counts++;
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
i_counts=i_counts-2;
}
else
{
cout<<"读取文件夹失败!"<<endl;
}
cout<<"文件总数:"<<i_counts<<endl;
}
运行结果