網站首頁 編程語言 正文
一般來說,獲取指定文件夾下的所有文件名,用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-12-25 Python中你所不知道的星號?*?用法_python
- 2022-04-30 C語言鏈表實現商品庫存管理系統_C 語言
- 2023-02-27 Python獲取"3年前的今天"的日期時間問題_python
- 2022-04-02 IDEA集成Docker實現打包的方法_docker
- 2023-05-29 Python?input輸入超時選擇默認值自動跳過問題_python
- 2023-10-28 go帶緩沖chan實現消息隊列功能_Golang
- 2022-10-17 shell腳本的流程控制語句的實現_linux shell
- 2022-11-19 通過?C#/VB.NET?代碼將?Excel?工作表拆分為單獨的文件_C#教程
- 最近更新
-
- 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同步修改后的遠程分支