lundi 17 juillet 2017

Use a variable inside a string

In my program, part of the resources needed is a directory to store data. As is custom, I decided to make this directory ~/.program/. In c++, the correct way to make this directory (on UNIX-based systems) is this code:

#include <sys/stat.h>
#include <unistd.h>
#include <iostream>

using namespace std;

void mkworkdir()
{
    if(stat("~/.program",&st) == 0)
    {
        cout << "Creating working directory..." << endl;
        mkdir("~/.program/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
        mkdir("~/.program/moredata", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    }

    else
    {
        cout << "Working directory found... continuing" << endl;
    }
}

int main()
{
    mkworkdir();
    return 0;
}

Now, the reliability of using the ~ in mkdir("~/.program/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) is questionable at the least, so what I actually want to do is prompt for the username, store that in a string (like string usern; cin >> usern;), and then do mkdir("/home/{$USERN}/.program/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) (like in shell). However, I have no idea how to get some equivalent of $USERN into a string, as in, I don't know how to get an expandable c++ construction into a string. What I mean with that is that I insert whatever "form" of variable would get expanded into the contents of that variable into the string.

I apologize if this question is confusing, I just can't seem to be able to explain well what exactly it is that I want.

Alternatively, and much more preferably, would it be possible to get the username without prompting for it? (and store this in a string, of course)

Aucun commentaire:

Enregistrer un commentaire