I'm doing a program to management of directories and their contents and I want to run a program in determinate directory and load this directory and all the files contained to memory. And return true in case of bee successful. Example I have a folder that contain 3 files(center.txt, main.csv, re.txt) and 2 folders(1, 2) inside folder 1 is the file(file.txt). I want load this to memory, the files and the directory( and the files that determinate directory contains). I want to do I pre-run the directory and while find a file load them to memory.
I don't know, I'm not on a linux system right now. I'm just letting you know that rdbuf() is a way to put the contents of a file into memory as a string or stringstream. Also make sure you include the necessary headers like #include <fstream> #include <sstream>
I'd assume you want to open a different file each time; your while loop is opening the same file each time, I think.
Currently, you are setting your path, c, before you loop over readdir. But you need to re-set c (and f) each time in the while loop, with the new value of entry->d_name. If my memory is correct, readdir returns each file as well, and not just directories.
I can't test this, but try something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
while (entry = readdir(pDIR))
{
constchar* child_path = entry->d_name;
std::ifstream in(child_path);
if (in)
{
std::string str(static_cast<std::stringstream const&>(std::stringstream() << in.rdbuf()).str());
std::cout << str << "\n";
}
else
{
std::cout << "cannot open " << child_path << "\n";
}
}
closedir(pDIR); // put this BEFORE you return, to properly clean-up