zoukankan      html  css  js  c++  java
  • C++获取指定文件夹下的所有文件名(或路径)

     
    1. #include <io.h>  
    2. #include <fstream>  
    3. #include <string>  
    4. #include <vector>  
    5. #include <iostream>  
    6. using namespace std;  
    7.   
    8.   //其实两个字符串连在一起比如string可惜写成:str1+str2+“xxx”。。。
    9. //获取所有的文件名  
    10. void GetAllFiles( string path, vector<string>& files)    
    11. {    
    12.   
    13.     long   hFile   =   0;    
    14.     //文件信息    
    15.     struct _finddata_t fileinfo;//用来存储文件信息的结构体    
    16.     string p;    
    17.     if((hFile = _findfirst(p.assign(path).append("\*").c_str(),&fileinfo)) !=  -1)  //第一次查找  
    18.     {    
    19.         do    
    20.         {     
    21.             if((fileinfo.attrib &  _A_SUBDIR))  //如果查找到的是文件夹  
    22.             {    
    23.                 if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)  //进入文件夹查找  
    24.                 {  
    25.                     files.push_back(p.assign(path).append("\").append(fileinfo.name) );  
    26.                     GetAllFiles( p.assign(path).append("\").append(fileinfo.name), files );   
    27.                 }  
    28.             }    
    29.             else //如果查找到的不是是文件夹   
    30.             {    
    31.                 files.push_back(p.assign(fileinfo.name) );  //将文件路径保存,也可以只保存文件名:    p.assign(path).append("\").append(fileinfo.name)  
    32.             }   
    33.   
    34.         }while(_findnext(hFile, &fileinfo)  == 0);    
    35.   
    36.         _findclose(hFile); //结束查找  
    37.     }   
    38.   
    39. }    
    40.   
    41. //获取特定格式的文件名  
    42. void GetAllFormatFiles( string path, vector<string>& files,string format)    
    43. {    
    44.     //文件句柄    
    45.     long   hFile   =   0;    
    46.     //文件信息    
    47.     struct _finddata_t fileinfo;    
    48.     string p;    
    49.     if((hFile = _findfirst(p.assign(path).append("\*" + format).c_str(),&fileinfo)) !=  -1)    
    50.     {    
    51.         do    
    52.         {      
    53.             if((fileinfo.attrib &  _A_SUBDIR))    
    54.             {    
    55.                 if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)    
    56.                 {  
    57.                     //files.push_back(p.assign(path).append("\").append(fileinfo.name) );  
    58.                     GetAllFormatFiles( p.assign(path).append("\").append(fileinfo.name), files,format);   
    59.                 }  
    60.             }    
    61.             else    
    62.             {    
    63.                 files.push_back( p.assign(fileinfo.name));  //将文件路径保存,也可以只保存文件名:    p.assign(path).append("\").append(fileinfo.name)  
    64.             }    
    65.         }while(_findnext(hFile, &fileinfo)  == 0);    
    66.   
    67.         _findclose(hFile);   
    68.     }   
    69. }   
    70.   
    71. // 该函数有两个参数,第一个为路径字符串(string类型,最好为绝对路径);  
    72. // 第二个参数为文件夹与文件名称存储变量(vector类型,引用传递)。  
    73. // 在主函数中调用格式(并将结果保存在文件"AllFiles.txt"中,第一行为总数):  
    74.   
    75. int main()  
    76. {  
    77.     string filePath = "D:\文档\课程\数据结构\MOOC练习\testPCL\x64\Debug";    
    78.     vector<string> files;    
    79.     char * distAll = "AllFiles.txt";  
    80.   
    81.     //读取所有的文件,包括子文件的文件  
    82.     //GetAllFiles(filePath, files);  
    83.   
    84.     //读取所有格式为jpg的文件  
    85.     string format = ".dll";  
    86.     GetAllFormatFiles(filePath, files,format);  
    87.     ofstream ofn(distAll);  
    88.     int size = files.size();   
    89.     ofn<<size<<endl;  
    90.     for (int i = 0;i<size;i++)    
    91.     {    
    92.         ofn<<files[i]<<endl; // 写入文件  
    93.         cout<< files[i] << endl;//输出到屏幕  
    94.     }  
    95.     ofn.close();  
    96.     return 0;  
    97. }  
  • 相关阅读:
    U-Boot新手入门
    安装交叉编译工具
    Makefile 工程管理
    gcc基本用法
    poj 3264 Balanced Lineup
    hdoj 1166 敌兵布阵
    poj 1363 Rails
    poj 1028 Web Navigation
    zoj 3621 Factorial Problem in Base K
    poj1861最小生成树
  • 原文地址:https://www.cnblogs.com/1996313xjf/p/5918456.html
Copyright © 2011-2022 走看看