不用filesystem写
时间: 2024-02-02 18:11:28 浏览: 26
好的,可以使用标准的C文件操作库来实现。以下是代码示例:
```c++
#include <iostream>
#include <vector>
#include <dirent.h>
int main() {
std::vector<std::string> filenames;
std::string path = "your_folder_path";
DIR* dir;
struct dirent* ent;
if ((dir = opendir(path.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_REG) {
filenames.push_back(ent->d_name);
}
}
closedir(dir);
}
for (const auto& filename : filenames) {
std::cout << filename << std::endl;
}
return 0;
}
```
其中,`your_folder_path` 是你需要遍历的文件夹路径。代码中使用 `opendir` 打开文件夹,`readdir` 循环读取文件夹中的所有文件和子文件夹,使用 `ent->d_type == DT_REG` 判断是否为文件,如果是则将文件名添加到 `filenames` 向量中。最后遍历 `filenames` 向量并输出文件名。需要注意的是,这种方法只适用于Unix系统,不适用于Windows系统。