怎么用C语言编程遍历文件夹下所有文件名

如题所述

     
  /**************************************************   
        è¿™æ˜¯CBrowseDir的类定义文件   BrowseDir.h     
     
  /**************************************************   
  #include   "stdlib.h"   
     
  class   CBrowseDir   
  {   
  protected:   
  //存放初始目录的绝对路径,以'\'结尾   
  char   m_szInitDir[_MAX_PATH];   
     
  public:   
  //缺省构造器   
  CBrowseDir();   
     
  //设置初始目录为dir,如果返回false,表示目录不可用   
  bool   SetInitDir(const   char   *dir);   
     
  //开始遍历初始目录及其子目录下由filespec指定类型的文件   
  //filespec可以使用通配符   *   ?,不能包含路径。   
  //如果返回false,表示遍历过程被用户中止   
  bool   BeginBrowse(const   char   *filespec);   
     
  protected:   
  //遍历目录dir下由filespec指定的文件   
  //对于子目录,采用迭代的方法   
  //如果返回false,表示中止遍历文件   
  bool   BrowseDir(const   char   *dir,const   char   *filespec);   
     
  //函数BrowseDir每找到一个文件,就调用ProcessFile   
  //并把文件名作为参数传递过去   
  //如果返回false,表示中止遍历文件   
  //用户可以覆写该函数,加入自己的处理代码   
  virtual   bool   ProcessFile(const   char   *filename);   
     
  //函数BrowseDir每进入一个目录,就调用ProcessDir   
  //并把正在处理的目录名及上一级目录名作为参数传递过去   
  //如果正在处理的是初始目录,则parentdir=NULL   
  //用户可以覆写该函数,加入自己的处理代码   
  //比如用户可以在这里统计子目录的个数   
  virtual   void   ProcessDir(const   char     
  *currentdir,const   char   *parentdir);   
  };   
     
     
  /*********************************************/   
     
  è¿™æ˜¯CBrowseDir的类实现文件   BrowseDir.cpp   
     
  /***********************************************/   
  #include   "stdlib.h"   
  #include   "direct.h"   
  #include   "string.h"   
  #include   "io.h"   
     
  #include   "browsedir.h"   
     
  CBrowseDir::CBrowseDir()   
  {   
  //用当前目录初始化m_szInitDir   
  getcwd(m_szInitDir,_MAX_PATH);   
     
  //如果目录的最后一个字母不是'\',则在最后加上一个'\'   
  int   len=strlen(m_szInitDir);   
  if   (m_szInitDir[len-1]   !=   '\\')   
  strcat(m_szInitDir,"\\");   
  }   
     
  bool   CBrowseDir::SetInitDir(const   char   *dir)   
  {   
  //先把dir转换为绝对路径   
  if   (_fullpath(m_szInitDir,dir,_MAX_PATH)   ==   NULL)   
  return   false;   
     
  //判断目录是否存在   
  if   (_chdir(m_szInitDir)   !=   0)   
  return   false;   
     
  //如果目录的最后一个字母不是'\',则在最后加上一个'\'   
  int   len=strlen(m_szInitDir);   
  if   (m_szInitDir[len-1]   !=   '\\')   
  strcat(m_szInitDir,"\\");   
     
  return   true;   
  }   
     
  bool   CBrowseDir::BeginBrowse(const   char   *filespec)   
  {   
  ProcessDir(m_szInitDir,NULL);   
  return   BrowseDir(m_szInitDir,filespec);   
  }   
     
  bool   CBrowseDir::BrowseDir   
  (const   char   *dir,const   char   *filespec)   
  {   
  _chdir(dir);   
     
  //首先查找dir中符合要求的文件   
  long   hFile;   
  _finddata_t   fileinfo;   
  if   ((hFile=_findfirst(filespec,&fileinfo))   !=   -1)   
  {   
  do   
  {   
  //检查是不是目录   
  //如果不是,则进行处理   
  if   (!(fileinfo.attrib   &   _A_SUBDIR))   
  {   
  char   filename[_MAX_PATH];   
  strcpy(filename,dir);   
  strcat(filename,fileinfo.name);   
  if   (!ProcessFile(filename))   
  return   false;   
  }   
  }   while   (_findnext(hFile,&fileinfo)   ==   0);   
  _findclose(hFile);   
  }   
     
  //查找dir中的子目录   
  //因为在处理dir中的文件时,派生类的ProcessFile有可能改变了   
  //当前目录,因此还要重新设置当前目录为dir。   
  //执行过_findfirst后,可能系统记录下了相关信息,因此改变目录   
  //对_findnext没有影响。   
  _chdir(dir);   
  if   ((hFile=_findfirst("*.*",&fileinfo))   !=   -1)   
  {   
  do   
  {   
  //检查是不是目录   
  //如果是,再检查是不是   .   æˆ–   ..     
  //如果不是,进行迭代   
  if   ((fileinfo.attrib   &   _A_SUBDIR))   
  {   
  if   (strcmp(fileinfo.name,".")   !=   0   &&   strcmp   
  (fileinfo.name,"..")   !=   0)   
  {   
  char   subdir[_MAX_PATH];   
  strcpy(subdir,dir);   
  strcat(subdir,fileinfo.name);   
  strcat(subdir,"\\");   
  ProcessDir(subdir,dir);   
  if   (!BrowseDir(subdir,filespec))   
  return   false;   
  }   
  }   
      }   while   (_findnext(hFile,&fileinfo)   ==   0);   
  _findclose(hFile);   
  }   
  return   true;   
  }   
     
  bool   CBrowseDir::ProcessFile(const   char   *filename)   
  {   
  return   true;   
  }   
     
  void   CBrowseDir::ProcessDir(const   char     
  *currentdir,const   char   *parentdir)   
  {   
  }   
     
     
  /*************************************************   
  è¿™æ˜¯ä¾‹å­example.cpp     
         
  /*************************************************   
  #include   "stdio.h"   
     
  #include   "BrowseDir.h"   
     
  //从CBrowseDir派生出的子类,用来统计目录中的文件及子目录个数   
  class   CStatDir:public   CBrowseDir   
  {   
  protected:   
  int   m_nFileCount;       //保存文件个数   
  int   m_nSubdirCount;   //保存子目录个数   
     
  public:   
  //缺省构造器   
  CStatDir()   
  {   
  //初始化数据成员m_nFileCount和m_nSubdirCount   
  m_nFileCount=m_nSubdirCount=0;   
  }   
     
  //返回文件个数   
  int   GetFileCount()   
  {   
  return   m_nFileCount;   
  }   
     
  //返回子目录个数   
  int   GetSubdirCount()   
  {   
  //因为进入初始目录时,也会调用函数ProcessDir,   
  //所以减1后才是真正的子目录个数。   
  return   m_nSubdirCount-1;   
  }   
     
  protected:   
  //覆写虚函数ProcessFile,每调用一次,文件个数加1   
  virtual   bool   ProcessFile(const   char   *filename)   
  {   
  m_nFileCount++;   
  return   CBrowseDir::ProcessFile(filename);   
  }   
     
  //覆写虚函数ProcessDir,每调用一次,子目录个数加1   
  virtual   void   ProcessDir   
  (const   char   *currentdir,const   char   *parentdir)   
  {   
  m_nSubdirCount++;   
  CBrowseDir::ProcessDir(currentdir,parentdir);   
  }   
  };   
     
  void   main()   
  {   
  //获取目录名   
  char   buf[256];   
  printf("请输入要统计的目录名:");   
  gets(buf);   
     
  //构造类对象   
  CStatDir   statdir;   
     
  //设置要遍历的目录   
  if   (!statdir.SetInitDir(buf))   
  {   
  puts("目录不存在。");   
  return;   
  }   
     
  //开始遍历   
  statdir.BeginBrowse("*.*");   
     
  //统计结果中,子目录个数不含   .   åŠ   ..   
  printf("文件总数:   %d\n子目录总数:     
  %d\n",statdir.GetFileCount(),   
  statdir.GetSubdirCount());   
  }
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-05-24
运行结果为树形输出方式。
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <stack>

#include <windows.h>

using namespace std;

/*
* 遍历lpszPath下所有文件及文件夹,并按顺序显示其中的内容.
*/
/*
* 如果扫描到文件夹,则将其存入 Dirs 队列中,并显示名称,
* 如果扫描到文件,则显示其名称;
* 当前文件夹处理完毕后就处理其下一级文件夹,下一级文件夹从队
* 列中得到.
*/

void function( LPCTSTR lpszPath,ostream & out)
{
//开始查找;
stack<TCHAR*> Dirs;
stack<int> DirDepth;

TCHAR *tmp=new TCHAR[lstrlen(lpszPath)+1];
lstrcpy(tmp,lpszPath);

if(tmp[lstrlen(tmp)-1]=='\\')
tmp[lstrlen(tmp)-1]='\0';

TCHAR szFind[MAX_PATH*2];
TCHAR szFile[MAX_PATH*2];
TCHAR *curdir;
int curdepth=1; //当前文件夹的深度

Dirs.push(tmp);
DirDepth.push(curdepth);

for(;!Dirs.empty();)
{
curdir=Dirs.top();
curdepth=DirDepth.top();
Dirs.pop();
DirDepth.pop();

lstrcpy(szFind,curdir);
lstrcat(szFind, "\\*.*"); // 找所有文件
WIN32_FIND_DATA wfd;

HANDLE hFind = FindFirstFile(szFind, &wfd);

if (hFind != INVALID_HANDLE_VALUE) // 如果找到
{
if (curdepth >1)
out<<" ";
for(int i=1;i<curdepth-1;++i)
out<<'|'<<" ";
out<<'+'<<curdir<<endl;

do
{

if (wfd.cFileName[0] == '.')
continue; // 过滤"." ".." 这两个目录

if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
wsprintf(szFile, "%s\\%s", curdir, wfd.cFileName);

//function(szFile); // 如果找到的是目录,则进入此目录进行递归
TCHAR* tmp=new TCHAR[lstrlen(szFile)+2];
lstrcpy(tmp,szFile);
Dirs.push(tmp);
DirDepth.push(curdepth+1);
}
else
{
//输出文件名
out<<" ";
for(int i=1;i<curdepth;++i)
out<<'|'<<" ";
out<<wfd.cFileName<<endl;
}
} while (FindNextFile(hFind, &wfd));

}// if

delete [] curdir;

FindClose(hFind); // 关闭查找句柄

}// for()
}

int main(int argc,char *argv[])
{
ofstream fout("遍历结果.txt");
if(argc<=1)
{
cerr<<endl<<"文件夹遍历,请输入路径:";
TCHAR path[MAX_PATH];
cin>>path;
function(path,fout);
}
else
{
function(argv[1],fout);
}

return 0;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网