Search code examples
c++raspberry-pimkdir

mkdir() isnt working with absolute filepath


I am using a raspberry pi to store data collected from a rocket launch. I am trying to create a directory which has the date and time built in, to hold multiple text and .csv files which hold interesting data. My code looks like:

string date = getDateTime(); //Returns a string like "Launch_2017_04_28_23:31:03"
string dated_directory = "~/Launch_System_Cpp/Source_Code/Launch_Data/" + date;
    cout << dated_directory << endl;
    if (mkdir(dated_directory.c_str(), ACCESSPERMS )) {
            cout << "ERROR creating dated directory" << endl;
    }

The executable itself is located in ~/Launch_Code_Cpp/. I have been able to get this working with relative references, but I want this code to work no matter the directory it was run from. I haven't been able to get it working with absolute references; it always enters the if statement, and the directory doesn't exist when the program exits.

Can you tell me what I may be doing wrong when I try to make this directory? Is there a better way to make this directory?


Solution

  • The ~ character is converted to the home directory by the shell. Since you're not using a shell, you need to expand that yourself.