zoukankan      html  css  js  c++  java
  • C++ 获取文件夹下的所有文件名

    原文:http://blog.csdn.net/cxf7394373/article/details/7195661

     1 #include<iostream>
     2 #include<fstream>
     3 #include<vector>
     4 #include<string.h>
     5 #include<io.h>
     6 using namespace std;
     7 void getfiles(string filepath, vector<string> &files);
     8 
     9 int main() {
    10     fstream file;
    11     vector<string> files;
    12     string filepath = "D:\学习资料";
    13     getfiles(filepath, files);
    14     int size = files.size();
    15     for (int i = 0; i < size; i++) {
    16         cout << files[i].c_str() << endl;
    17     }
    18     return 0;
    19 }
    20 
    21 void getfiles(string path, vector<string>& files) {
    22     //文件句柄
    23     long hFile = 0;
    24     //文件信息
    25     struct _finddata_t fileinfo;
    26     string p;
    27     if ((hFile = _findfirst(p.assign(path).append("\*").c_str(), &fileinfo))
    28             != -1) {
    29         do {
    30             //如果是目录,迭代之
    31             //如果不是,加入列表
    32             if ((fileinfo.attrib & _A_SUBDIR)) {
    33                 if (strcmp(fileinfo.name, ".") != 0
    34                         && strcmp(fileinfo.name, "..") != 0)
    35                     getfiles(p.assign(path).append("\").append(fileinfo.name),
    36                             files);
    37             } else {
    38                 files.push_back(
    39                         p.assign(path).append("\").append(fileinfo.name));
    40             }
    41         } while (_findnext(hFile, &fileinfo) == 0);
    42         _findclose(hFile);
    43     }
    44 }
  • 相关阅读:
    create-react-app 修改 webpack output.publicPath
    洛谷 P1282 多米诺骨牌 (01背包)
    UVa 1627
    UVa 1626
    UVa 11584
    UVa 11400
    UVa 116
    UVa 1347 Tour (dp)
    树形背包小结
    数据流图题目一
  • 原文地址:https://www.cnblogs.com/sindy/p/5025313.html
Copyright © 2011-2022 走看看