C++ 遍历文件夹

来源:互联网 发布:穿越火线网络环境异常 编辑:程序博客网 时间:2024/05/29 18:53

我写的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;


}


原创粉丝点击