用C遍历文件夹下的所有文件

来源:互联网 发布:信阳师院网络教育平台 编辑:程序博客网 时间:2024/05/16 09:44

<转>http://hi.baidu.com/xiakeyou/blog/item/7e605601227f3506728da5bd.html

#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)))

    {

        if(!strcmp(filefind.name,".."))continue;

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

        if ((_A_SUBDIR==filefind.attrib))

        {     

            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;

}

0 0
原创粉丝点击