C++(17):获取启动程序的路径_获得 启动路径 vc-CSDN博客
下面的代码会遍历当前目录下的子文件夹data_in中的txt文件:
#include <iostream>
#include <string>
#include <filesystem> //path
#include <vector>
#include<regex> //正则表达式
namespace fs = std::filesystem;
using namespace std;
vector<string> get_all_file_name(const string & strPath)
{
vector<string> vecFilePath;
regex fileSuffix("(.*)");
for (auto&itr : fs::directory_iterator(strPath))
{
auto filepath = itr.path();
//auto filename = filepath.filename();//filename.string() <=> "1.txt"
//auto s = filename.replace_extension().string();// s = "1" 去掉后缀
vecFilePath.push_back(filepath.string());//filepath.string() <=> "./data_in/1.txt"
cout << filepath << endl;
}
return vecFilePath;
}
输出如下:
当前目录中有一个data_in子目录:
data_in内的文件:
VS开启使用C++17