微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c++在windows、linux下获取指定文件夹下所有文件名的方法

一般来说,获取指定文件夹下的所有文件名,用python是较为方便的,直接:

import os

files_name = os.listdir(“一个路径”)

但也有c++程序偶尔也有这个需求,下面就直接上c++在windows和linux去读取文件夹下文件名的方法,不同的系统代码上有一些差别

Windows(vs)

vs的环境,主要是用到了头文件<io.h>,还有以下的几点说明,大伙可以按需修改,我在代码中也做了详细的注释:

  • 这个遇到文件夹会回归调用,所以如果不想让其进入,就在找到文件夹时直接continue;
  • 保存的仅仅是文件名,也可以保存绝对路径,在下面的else中改一下就好了;
  • 当然可以加个format格式参数,就只保留想要的后缀的文件,就自己去改了。
#include <iostream>
#include <vector>
#include <string>
#include <io.h>

// 可在这函数中再加一个format格式参数,push到vector前判断下文件名后缀,仅保留指定格式
void GetAllFiles(std::string path, std::vector<std::string> &files) {
	// 用来存储文件信息的结构体,在头文件 <io.h>
	struct _finddata_t fileinfo;  // _finddata_t 这是一个struct类,c++中可以不要前面的struct的

	intptr_t hFile = 0;  
	
	std::string p;  // 不在这p(path)初始化
	// 第一次查找
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
		do {
			// 如果找到的是文件
			if ((fileinfo.attrib & _A_SUBDIR)) {
				// 不想进入文件夹,就在这里continue
				if (std::strcmp(fileinfo.name, ".") != 0 && std::strcmp(fileinfo.name, "..") != 0) {
					// 进入查找
					files.push_back(p.assign(path).append("\\").append(fileinfo.name));
					GetAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
				}
			}
			// 如果找到的不是文件
			else {
			    // 保存的是文件
				files.push_back(p.assign(fileinfo.name));  
				// 也可以是保存绝对路径
				// files.push_back(p.assign(path).append("\\").append(fileinfo.name));  
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		// 结束查找
		_findclose(hFile);
	}

}

int main(int argc, char* argv[]) {
	std::string file_path = "E:\\PycharmProject\\Wrench\\screwLine_demo";  
	std::vector<std::string> files_name;

	GetAllFiles(file_path, files_name);
	for (auto k : files_name) {
		std::cout << k << std::endl;
	}
	system("pause");
	return 0;
}

效果

不同方式的结果图

Linux

  • io.h 头文件可能不兼容跨平台操作。在windows下这个头文件运行稳定,但是在linux下这个头文件有点不一样,就换了下;
  • linux需要头文件<dirent.h>
  • 这个代码不会进到所给文件夹里面,只会把给定文件夹下的文件夹名、文件名列出来,像python的os.listdir(),有需要的话,自己改一下就好了。
#include <iostream>
#include <vector>
#include <sys/types.h>
#include <dirent.h>  
#include <cstring>

void GetFileName(std::string path, std::vector<std::string> &files) {
    DIR *pDir;   //  是头文件<dirent.h>的类型
    struct dirent *ptr;  // opendir、readdir这些都是头文件dirent.h
    if (!(pDir = opendir(path.c_str()))) return;
    while ((ptr = readdir(pDir)) != 0) {
        // strcmp是C语言里的,只导入string,然后std::strcmp都是没有的
        if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
            files.push_back(path + "/" + ptr->d_name);  // 可以只保留名字
        }
    }
    closedir(pDir);
}

int main(int argc, char* argv[]) {
    std::string file_path = "/home/nian/123";
    std::vector<std::string> files_name;

    GetFileName(file_path, files_name);

    for (auto iter = files_name.cbegin(); iter != files_name.cend(); ++iter) {
        std::cout << *iter << std::endl;
    }
    return 0;
}

希望能帮到你。

原文地址:https://www.jb51.cc/wenti/3281130.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐