Question: How to recursively store all files into a vector?
fundies opened this issue · comments
I'm trying to recursively search a folder for files and then store them into a vector for later use but my vector is coming up empty. if I cout resources inside the else it does list files though. Any ideas what I'm doing wrong?
include "tinydir.h"
include
include
include
typedef std::vectorstd::string fpathVec;
fpathVec sprites, rooms;
void get_files(std::string fpath, fpathVec vec)
{
tinydir_dir dir;
int i;
if (fpath.back() != '/')
fpath += "/";
tinydir_open_sorted(&dir, fpath.c_str());
for (i = 0; i < dir.n_files; i++)
{
tinydir_file file;
tinydir_readfile_n(&dir, &file, i);
if (file.is_dir)
{
if (strcmp(file.name, ".") != 0 && strcmp(file.name, "..") != 0)
{
get_files(fpath + file.name + "/", vec);
}
}
else
{
std::string resource = fpath + file.name;
vec.push_back(resource);
}
}
tinydir_close(&dir);
}
int main()
{
get_files("../../gaym", sprites);
std::cout << sprites.size() << std::endl;
}
You're passing a copy of a std::vector into the function, so the original copy is never modified.
Change the function signature to this to pass a reference:
void get_files(std::string fpath, fpathVec &vec)