用FindFirstFile和FindNestFile编写一个遍历文件夹下所有文件的代码怎么写,求大神指导,万分感谢(C++)

如题所述

递归遍历一下就行了,代码:

int ShowDir(string strDir)
{
 struct _finddata_t c_file;
    long hFile;
 if((hFile = _findfirst( strDir.c_str(), &c_file )) == -1L )
 {   
  return -1;
 }
 while( _findnext( hFile, &c_file ) == 0 )
 {
  if (strcmp(".", c_file.name) == 0 || strcmp("..", c_file.name) == 0)
  {
   continue;
  }
  
  if (c_file.attrib & _A_SUBDIR)
  {
   //如果是子文件夹,递归调用 
  string strSubDir = strDir.substr(0, strDir.length() - 3) + c_file.name + "\\*.*";
   ShowDir(strSubDir);
  }
  else
  {
   printf("Name:%-20s  LastWrite:%s", c_file.name, ctime(&(c_file.time_write)));
  }  
 }
 _findclose( hFile );
 return 0;
}

其它的头文件和调用的还和之前回答你的那个问题一样,你把打印的格式什么的自己改下就行了,比如把路径或者文件夹名称打出来都可以。

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

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