C++读取文件夹中文件名以及文件路径
时间: 2023-08-30 16:09:07 浏览: 130
可以使用 dirent.h 头文件中的 DIR 和 dirent 结构体,实现读取文件夹中的文件名和路径的功能。具体实现可以参考以下的示例代码:
```c
#include <stdio.h>
#include <dirent.h>
int main() {
char *dir_path = "./dir"; // 文件夹路径
DIR *dir;
struct dirent *ent;
if ((dir = opendir(dir_path)) != NULL) {
// 遍历文件夹中的文件
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_REG) { // 只处理普通文件
printf("%s/%s\n", dir_path, ent->d_name); // 打印文件路径和文件名
}
}
closedir(dir);
} else {
printf("Failed to open directory %s\n", dir_path);
}
return 0;
}
```
这段代码可以打印出文件夹 "./dir" 中的所有普通文件的路径和文件名。
相关问题
c++批量读取文件夹中的图片
您可以使用Python中的Pillow库来实现批量读取文件夹中的图片。以下是一个简单的示例代码:
```python
from PIL import Image
import os
# 指定文件夹路径
folder_path = '/path/to/folder/'
# 遍历文件夹中所有图片文件
for filename in os.listdir(folder_path):
if filename.endswith('.png') or filename.endswith('.jpg'):
# 读取图片
img = Image.open(os.path.join(folder_path, filename))
# 处理图片,例如进行识别或转换格式等
# ...
```
在上面的示例中,`os.listdir()`可以列出指定文件夹中的所有文件和文件夹,而`endswith()`可以用来判断文件名是否以特定字符串结尾,从而只读取图片文件。每处理一张图片,您可以进行一些自己的操作,例如进行图像识别或转换格式等。
C++实现将文件夹中所有文件按照文件名分别读取到三个容器中
要将文件夹中的所有文件按照文件名分别读取到三个容器中,您可以使用C++的标准库和文件系统库。以下是一种实现方式:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <filesystem>
void readFilesInFolder(const std::string& folderPath, std::vector<std::string>& container1, std::vector<std::string>& container2, std::vector<std::string>& container3) {
std::filesystem::directory_iterator endIter;
for (std::filesystem::directory_iterator iter(folderPath); iter != endIter; ++iter) {
if (std::filesystem::is_regular_file(iter->status())) {
std::string filename = iter->path().filename().string();
// 将文件名按照一定规则分别添加到三个容器中
if (filename.size() >= 9) {
container1.push_back(filename.substr(0, 9));
}
if (filename.size() >= 6) {
container2.push_back(filename.substr(0, 6));
}
if (filename.size() >= 3) {
container3.push_back(filename.substr(0, 3));
}
}
}
}
int main() {
std::string folderPath = "path/to/folder";
std::vector<std::string> container1, container2, container3;
readFilesInFolder(folderPath, container1, container2, container3);
// 打印容器中的内容
std::cout << "Container 1:" << std::endl;
for (const auto& item : container1) {
std::cout << item << std::endl;
}
std::cout << "Container 2:" << std::endl;
for (const auto& item : container2) {
std::cout << item << std::endl;
}
std::cout << "Container 3:" << std::endl;
for (const auto& item : container3) {
std::cout << item << std::endl;
}
return 0;
}
```
在上述代码中,`readFilesInFolder`函数接受文件夹路径、三个容器的引用作为输入参数。它使用`std::filesystem`库遍历文件夹中的所有文件,将文件名按照一定规则分别添加到三个容器中。
在主函数中,我们示范了如何使用`readFilesInFolder`函数来读取文件夹中的所有文件,并将它们按照文件名分别存放到三个容器中。然后,我们打印出每个容器中的内容。
请注意,上述代码假设您已经包含了必要的头文件,并将文件夹路径替换为实际的文件夹路径。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)