首页 > 编程语言 >C++ 获取目录下所有文件的文件名

C++ 获取目录下所有文件的文件名

时间:2022-11-10 21:11:16浏览次数:40  
标签:files name 文件名 C++ 目录 base file ptr dir

vector<string> getFiles(string cate_dir)
{
    vector<string> files;//存放文件名

#ifdef WIN32
    _finddata_t file;
    long lf;
    //输入文件夹路径
    if ((lf = _findfirst(cate_dir.append("\\*").c_str(), &file)) == -1) {
        cout << cate_dir.c_str() << endl;
        cout << cate_dir << " not found!!!" << endl;
    }
    else {
        while (_findnext(lf, &file) == 0) {
            //输出文件名
            //cout<<file.name<<endl;
            if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)
                continue;
            files.push_back(file.name);
        }
    }
    _findclose(lf);
#endif

#ifdef linux
    DIR* dir;
    struct dirent* ptr;
    char base[1000];

    if ((dir = opendir(cate_dir.c_str())) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }

    while ((ptr = readdir(dir)) != NULL)
    {
        if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)    ///current dir OR parrent dir
            continue;
        else if (ptr->d_type == 8)    ///file
            //printf("d_name:%s/%s\n",basePath,ptr->d_name);
            files.push_back(ptr->d_name);
        else if (ptr->d_type == 10)    ///link file
            //printf("d_name:%s/%s\n",basePath,ptr->d_name);
            continue;
        else if (ptr->d_type == 4)    ///dir
        {
            files.push_back(ptr->d_name);
            /*
                memset(base,'\0',sizeof(base));
                strcpy(base,basePath);
                strcat(base,"/");
                strcat(base,ptr->d_nSame);
                readFileList(base);
            */
        }
    }
    closedir(dir);
#endif

    //排序,按从小到大排序
    sort(files.begin(), files.end());
    return files;
}
//检查该目录下的文件
string pmu_dir = "./data";
vector<string> files = getFiles(pmu_dir);

 

标签:files,name,文件名,C++,目录,base,file,ptr,dir
From: https://www.cnblogs.com/clayyjh/p/16878792.html

相关文章

  • shell脚本 查找出所有包含某关键词的文件名称,并输出第一个
    代码如下:#!/bin/basha=(`grep-r-l“thm_core”src/meta/connectivity`)echo${a[0]}#a是数组变量,需要注意的是“=”与“()”之间不能有空格#grep-r-l查找出在src/......
  • C++中fopen的句柄返回NULL
    我们在使用fopen打开文件的时候有时会出现失败返回null情况,但是我们不能直接通过log具体是什么原因导致的,所以这时我们可以通过errno和strerror获取错误码和错误信息。我......
  • linux 新建用户并只给目录的只读权限
    一、创建用户第一种方式:创建用户:addusername创建密码:passwdname(回车后出现修改密码的提示)该方式创建的用户目录默认在home下。第二种方式:useradd-d/usr/disp-......
  • C++编程笔记(GPU并行编程)
    目录一、配置并使用二、代码一、配置并使用环境:Windows10+CLion+VS2019cuda的安装,并行的话只需要安装cuda,cuDNN就不必了编译器设置,windows下建议使用MSVC,因为是官......
  • C++ 之string类常用接口功能解析(7千字长文带你玩懂string!)
    C++之string常用接口功能解析关于string的背景我们可以看到string的本质就是一个类模板!**为什么使用的是类模板因为字符串涉及到了编码的问题!——有ACCII,utf-8utf-1......
  • C++软件编码规范推荐--文件结构
    1文件结构每个C/C++程序通常分为两个文件,头文件(保存程序的声明)和定义文件(保持程序的实现)。头文件以“.h”为后缀;C程序的定义文件以“.c”为后缀,C++程序的定义文......
  • 各种编译环境中如何为C++添加命令行参数(Command-line parameter)
    因恐其内容丢失所以重新编辑到本博文中在实际的编程中,我们经常使用命令行参数。命令行参数的英文是Command-lineparameter或者是argument,下面是wikipedia中关于​​Command......
  • Web前端--Jquery获取域名的更目录
    jquery取得文件根目录functiongetRootPath(){//获得根目录varstrFullPath=window.document.location.href;varstrPath=window.document.location.pa......
  • C++'s most vexing parse
    本文地址https://www.cnblogs.com/wanger-sjtu/p/16876846.htmlC++'smostvexingparse是ScottMeyers在其名著《EffectiveSTL》中创造的一个术语。Scott用这个术......
  • C++语言程序设计课程设计任务书
    C++语言程序设计课程设计任务书一.课程设计的目的全面系统的学习面向对象程序设计的基本概念、基本语法和编程方法。正确理解掌握C++面向对象程序设计的基本特性:类、对......