用C++实现遍历文件夹下所有文件,windows ,linux 都能用

 
 
#include <iostream>
#include <io.h>
#include <string>
using  namespace  std;
void  dir(string path)
{
  long  hFile = 0;
  struct  _finddata_t  fileInfo;
  string pathName, exdName;
  // \\* 代表要遍历所有的类型
  if  ((hFile = _findfirst(pathName.assign(path).append( "\\*" ).c_str(), &fileInfo)) == -1) {
   return ;
  }
  do 
  {
   //判断文件的属性是文件夹还是文件
   cout << fileInfo.name << (fileInfo.attrib&_A_SUBDIR?  "[folder]" : "[file]" ) << endl;
  while  (_findnext(hFile, &fileInfo) == 0);
  _findclose(hFile);
  return ;
}
int  main()
{
  //要遍历的目录
  string path= "E:\\work\\zhidao\\test4" ;
  dir(path);
  system ( "pause" );
  return  0;
}
转自百度知道:https://zhidao.baidu.com/question/1798637132504240907.html?qbl=relate_question_0&word=C%2B%2B%B5%C3%B5%BD%CE%C4%BC%FE%BC%D0%CF%C2%CE%C4%BC%FE%CA%FD%C4%BF

猜你喜欢

转载自blog.csdn.net/sinat_31771313/article/details/79207105