我写的C++ 遍历文件夹的代码,比较简单,不要见笑。
#include <stdio.h>
#include <io.h>
#include <iostream>
#include <string>
using namespace std;
const string root = "E:\\corpusSenti_former\\";
void traverseFolder(string strRoot)
{
long handle;
struct _finddata_t fileInfo;
string rootPath = strRoot + "*.*";
handle = _findfirst(rootPath.c_str(),&fileInfo);
if (-1 == handle)
{
cout << "can not find file!" << endl;
exit(0);
}
else
{
do
{
if (strcmp(fileInfo.name,".") == 0 || strcmp(fileInfo.name,"..") == 0)
{
continue;
}
if (fileInfo.attrib == _A_SUBDIR)
{
cout << strRoot + fileInfo.name + "\\" << endl;
traverseFolder(strRoot + fileInfo.name + "\\");
}
else
{
cout << fileInfo.name << endl;
}
}
while (!_findnext(handle,&fileInfo));
}
_findclose(handle);
}
int main()
{
traverseFolder(root);
system("pause");
return 0;
}