網站首頁 編程語言 正文
一般來說,獲取指定文件夾下的所有文件名,用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://blog.csdn.net/nianjiuhuiyi/article/details/126498022
相關推薦
- 2022-01-21 Docker報錯:OCI runtime exec failed: exec failed: con
- 2022-06-21 使用Apache?Hudi?加速傳統的批處理模式的方法_Linux
- 2022-11-02 解析C/C++?Capstone?引擎源碼編譯問題_C 語言
- 2022-07-11 Reporting Classes中uvm_report_server的get_severity_c
- 2022-07-11 關于mv3 - Uncaught TypeError: Cannot read properties
- 2022-10-21 golang?一次性定時器Timer用法及實現原理詳解_Golang
- 2022-09-02 useEffect中不能使用async原理詳解_React
- 2022-07-17 oracle數據庫去除重復數據常用的方法總結_oracle
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支