How to load files into memory c++

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  void Load(const string &path) {
		DIR *pDIR;
		ifstream inn;
		string   str;
		string c = path;
		const char *f = c.c_str();
		struct dirent *entry;
		if (pDIR = opendir(f)) {
			while (entry = readdir(pDIR)) {
					cout << entry->d_name << "\n";

			}
			closedir(pDIR);
		}
	}
Once you have the path of the file, you can load the individual file into an std::string in memory using
1
2
3
4
5
std::ifstream in("the/path/to/file");
if (in)
    std::string str(static_cast<std::stringstream const&>(std::stringstream() << in.rdbuf()).str());
else
    // can't open file 

https://stackoverflow.com/a/116220

Like that?

bool Load(const string &path) {
DIR *pDIR;
ifstream inn;
string str;
string c = path;
const char *f = c.c_str();
struct dirent *entry;
if (pDIR = opendir(f)) {
while (entry = readdir(pDIR)) {
std::ifstream in(f);
if (in) {
std::string str(static_cast<std::stringstream const&>(std::stringstream() << in.rdbuf()).str());
cout << entry->d_name << "\n";
return true;
}
else {
return false;
}

}
closedir(pDIR);
}
}
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.
Last edited on
What I need to put in while loop?
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))
{
    const char* 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 


If that doesn't work or you're still having trouble, please reference
https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c
Last edited on
Topic archived. No new replies allowed.