适用于 Linux GNU C++,或 Windows Mingw-g++。不适用于 VC++
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
const string kIfDir = "/opt/blah/";// 文件夹路径
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
vector<string> fname_vec;
if( (dp=opendir(kIfDir.c_str()) )==NULL )
{
perror("open dir error");
}
while( (dirp=readdir(dp) )!=NULL )
{
if(dirp->d_name[0]=='.')// 跳过目录('.', '..')
{
continue;
}
fname_vec.push_back(dirp->d_name);
}
vector<string>::iterator it;
for(it = fname_vec.begin(); it != fname_vec.end(); ++it)
{
string ifpath = kIfDir + (*it);
// do something
}
closedir(dp);
return 0;
}