@AlanBirtles I think I've figured it out, it was entirely my own stupidity and a quirk of VS. When using Debug to test it the vector is always empty but when opening the .exe directly it does indeed work. Thank you for the help
Without a doubt this is down to the current working directory.
When debugging the current working directory is chosen by Visual Studio based on the project folder and debug-configuration. I think it's usually the output folder containing the binary being debugged, unless you override it.
What you probably want to do in your program, instead is avoid relying on the current working directory - unless of course you're designing a "interactive" CLI utility (think, like ls
) that is supposed to be "contextual" to the current directory.
BONUS
Since that older answer a lot has changed. Here's a version with std::filesystem
, ranges v3
and fmt
instead of Boost:
Compiler Explorer
#include <filesystem>
#include <iostream>
#include <fmt/ranges.h>
#include <fmt/printf.h>
#include <range/v3/all.hpp>
namespace fs = std::filesystem;
using paths = std::vector<fs::path>;
namespace r = ::ranges;
namespace v = r::views;
// return the paths of all files that have the specified extension in the
// specified directory and all subdirectories
auto find_ext(const fs::path& root, const std::string& ext) {
auto it = fs::recursive_directory_iterator(root, fs::directory_options::skip_permission_denied);
return r::subrange(it)
| v::transform(&fs::directory_entry::path)
| v::filter([](auto& p) { return fs::is_regular_file(p); })
| v::filter([=](auto& p) { return p.extension() == ext; })
;
}
auto find_ext(std::string ext) {
return [=](fs::path const& root) { return find_ext(root, ext); };
}
int main(int argc, char** argv) {
auto folders = r::subrange(argv+1, argv+argc);
for (auto&& fac : folders | v::transform(find_ext(".fac")) | v::join) {
fmt::print("File {} found in folder {}\n", fac.filename(), fac.parent_path());
}
}
When executed with e.g. ./test.exe . /tmp
:
File "dummy.fac" found in folder "./.git/logs/refs/heads"
File "dummy.fac" found in folder "./.git/logs/refs/remotes/gogs"
File "dummy.fac" found in folder "./.git/objects/b7"
File "dummy.fac" found in folder "./.git/objects/4b"
File "dummy.fac" found in folder "./.git/objects/63"
File "dummy.fac" found in folder "./.git/objects/36"
File "dummy.fac" found in folder "./.git/objects/af"
File "dummy.fac" found in folder "./.git/objects/17"
File "dummy.fac" found in folder "./.git/objects/7f"
File "dummy.fac" found in folder "./.git/objects/40"
File "dummy.fac" found in folder "/tmp/build-boost/boost/bin.v2/libs/timer/build/gcc-7/release/link-static"
File "dummy.fac" found in folder "/tmp/build-boost/boost/bin.v2/libs/nowide/build/gcc-7"
File "dummy.fac" found in folder "/tmp/build-boost/boost/bin.v2/libs/system/build/gcc-7/release/link-static/threading-multi"
File "dummy.fac" found in folder "/tmp/build-boost/boost/bin.v2/libs/locale/build/gcc-7/release/threadapi-pthread/threading-multi/visibility-hidden/util"
File "dummy.fac" found in folder "/tmp/.profiles/sehe/.cache/spotify/Data/1d"
File "dummy.fac" found in folder "/tmp/.profiles/sehe/.cache/spotify/Data/b1"
File "dummy.fac" found in folder "/tmp/.profiles/sehe/.cache/spotify/Storage/11"
File "dummy.fac" found in folder "/tmp/.profiles/sehe/.cache/spotify/Storage/c7"
File "dummy.fac" found in folder "/tmp/.font-unix"
As you can see I planted some dummy .fac files in random locations to test this.
Note also that I return path()
, not path().filename()
because otherwise you lose the location information.