您的位置:首页 > 编程语言 > C语言/C++

C语言遍历文件夹里所有文件【转】

2012-07-19 21:33 225 查看
#include<iostream>
#include<string>
#include<io.h>
using namespace std;

void   filesearch(string path,int layer)
{
struct _finddata_t   filefind;
string  curr=path+"\\*.*";
int   done=0,i,handle;
if((handle=_findfirst(curr.c_str(),&filefind))==-1)
return;

while(!(done=_findnext(handle,&filefind)))
{
printf("测试的--%s\n",filefind.name);
if(!strcmp(filefind.name,"..")){

continue;
}

for(i=0;i<layer;i++)
cout<<"     ";

if   ((_A_SUBDIR==filefind.attrib))
{
printf("----------%s\n",filefind.name);
cout<<filefind.name<<"(dir)"<<endl;
curr=path+"\\"+filefind.name;
filesearch(curr,layer+1);
}
else
{
cout<<filefind.name<<endl;
}
}
_findclose(handle);
}
int   main()
{
string   path;
cout<<"请输入目录"<<endl;
cin>>path;
filesearch(path,0);
system("PAUSE");
return   0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: