How to convert back to a string?

I am trying to create a class that scans a directory to find which files are in it and return the value as a string. But I can't convert the value of "value2" back to a string. How can I do this?

home.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <dirent.h>
#include <iostream>
#include <cstring>
using namespace std;

class Templates
{
public:
    string getTemplateDir();
	void getTemplates();
private:
	char *home_path;
};


home.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "home.h"

string Templates::getTemplateDir()
{
    home_path = getenv("HOME"); //Retrieve user's home directory
    string home = home_path; //Convert to string
    string template_dir = home + "/Templates";
    
    char *value1 = NULL;
    strcpy(value1, template_dir.c_str()); //Convert back to char
    
    DIR *value2 = NULL;
    value2 = opendir(value1);
    struct dirent *pent = NULL;
    
    if (value2 == NULL)
    {
    }

    while (pent = readdir(value2))
    {
        if (pent == NULL)
        {
        }
    }
    string all_templates = value2; //Here is where I don't know how to convert back to a string
    cout << pent->d_name << endl;
    closedir (tmp_dir);
	
    return all_templates;
}
You should use more meaningful variable names. Here's some suggestions:

1
2
3
value1 ==> dir_name
value2 ==> dir
pent   ==> entry

The problem is that "value2" is not a char* but a DIR*. You want to use "pent", which is a struct with a member called d_name containing the filename.

 
string all_templates = pent->d_name;

Thank you Hammurabi. Here is what I've been able to come up with so far. The only problem that I'm having with it now is that "." and ".." show up in the resulting string. I would like to get rid of those.

home.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "home.h"

string Templates::getTemplates()
{
    string all_templates; //Set the string that will be returned
	
    char *home_path;
    home_path = getenv("USERPROFILE"); //Get the user's home directory
	string home = home_path;  //Convert to std::string
	string template_path = home + "\\Templates";  //Add Templates directory to the end of home directory
	
	const char *template_dir = template_path.c_str();  //Convert back to a const char
	
	DIR *directory = NULL;
	directory = opendir(template_dir);  //Open the resulting directory
	struct dirent *entry = NULL;
	
    if (directory == NULL)
    {
    }

    while (entry = readdir(directory))
    {
        if (entry == NULL)
        {
        }
		all_templates = all_templates + entry->d_name + "\n";  //Add all the values into one string
    }
    closedir (directory);
	return all_templates;
}
1
2
3
4
5
6
    while (entry = readdir(directory))
    {
        if( strcmp( entry->d_name, "." ) && strcmp( entry->d_name, ".." ) )
            //Add all the values into one string
            all_templates = all_templates + entry->d_name + "\n";
    }

Topic archived. No new replies allowed.