打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++获取目录下的文件列表
#include   <iostream>  
#include   
<io.h>  
#include   
<direct.h>  
#include   
<string>  
#include   
<vector>  
#include   
<iomanip>  
#include   
<ctime>  
using   namespace   std;  

void getFiles( string, vector<string>& );

int   main()  {  
    vector
<string>   files;  

    getFiles( 
".", files );

    
// print the files get
    for   (int   j=0;   j<files.size();   ++j)  {
        cout   
<<   files[j] << endl; 
    }

        
    
return   0;  
}


void getFiles( string path, vector<string>& files ) {
    
//文件句柄  
    long   hFile   =   0;  
    
//文件信息  
    struct _finddata_t fileinfo;  

    
string p;

    
if   ((hFile   =   _findfirst(p.assign(path).append("/*").c_str(),&fileinfo))   !=   -1)  {  

        
do  {  
            
//如果是目录,迭代之
            
//如果不是,加入列表
            if   ((fileinfo.attrib   &   _A_SUBDIR)) {  
                
if   (strcmp(fileinfo.name,".")   !=   0   &&   strcmp(fileinfo.name,"..")   !=   0)  
                    getFiles(   p.assign(path).append(
"/").append(fileinfo.name), files   );  
            }
  else  {  
                files.push_back(   p.assign(path).append(
"/").append(fileinfo.name)  );
            }
  
        }
   while   (_findnext(   hFile,   &fileinfo   )   ==   0);  

        _findclose(hFile);  
    }

}

这是不影响当前目录的,
我看行,
网上有的是用了 _chdir 方法的,影响了当前程序状态,
也就是说,有的方法在用的时候你如果新建一个相对路径的话, 位置会不对,
而这个不会.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
bd-C++ 快速获取文件夹(目录)下的所有文件名(VS2013测试通过)
【C#】C#获取文件夹下的所有文件
【C#】C# TreeView操作
【20160924】GOCVHelper综述
漏洞 木马 cmdjsp.jsp
DirectoryInfo类
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服