With C++ 17 this is really easy.
Try the program below:
#include <iostream>
#include <filesystem>
#include <vector>
#include <iterator>
#include <algorithm>
namespace fs = std::filesystem;
int main(int argc, char* argv[]) {
// The start path. Use Program path
const fs::path startPath{ fs::path(argv[0]).parent_path() };
// Here we will store all file names
std::vector<fs::path> files{};
// Get all path names
std::copy_if(fs::directory_iterator(startPath), {}, std::back_inserter(files), [](const fs::directory_entry& de) { return de.path().extension() == ".txt"; });
// Output all files
for (const fs::path& p : files) std::cout << p.string() << '\n';
return 0;
}
We get the path name from argv[0] and then use the directory_iterator
to go through all the files.
Then, we will copy the pathname to our resulting files vector, if the extension is ".txt".
I am not sure, what I should explain further. In case of questions, please ask.