ofstream not working the way I want it to Ubuntu 12.04

I have a program which creates a configuration directory inside of a directory called "a" and puts a file inside the configuration directory, which of course would be the configuration file which is necessary to run the program. Without it, the program won't run. I currently have the following code (I know that system calls are bad practice, no need to scold me on that):
1
2
3
4
5
6
7
8
system("mkdir ~/a");
system("mkdir ~/a/test");
ofstream frankiscool;
frankiscool.open("~/a/root/test.txt");
string test="test";
frankiscool << test;
frankiscool.close();
cout << "finsihed!";

This creates the a and test directories (test is the configuration directory), although no file is created called test.txt. When I do it like this, it works:
1
2
3
4
5
6
7
8
system("mkdir ~/a");
system("mkdir ~/a/test");
ofstream frankiscool;
frankiscool.open("/home/jonny/a/test/test.txt");
string test="test";
frankiscool << test;
frankiscool.close();
cout << "finsihed!";

The problem is that I need it to make the file in the user's home directory; the user is probably not named jonny. I know that system calls are bad, but those are kind of a placeholder for a mkdir function I'm working on. Essentially, I just want to place a file in the user's home directory containing configuration info, although whenever I use ~ for the user's home directory, no file appears. I will not be able to make a customized program for every individual user for the path to their home directory; How can I get this to work?
Shouldn't
frankiscool.open("~/a/root/test.txt");
be
frankiscool.open("~/a/test/test.txt");
?

You can use environment variables to get the home directory of the user
std::getenv("HOME");
 
frankiscool.open("~/a/root/test.txt");


Also, I am fairly sure C++ doesn't understand the '~' symbol to mean the home directory. The reason it works with the system() command is because it is run by the shell (which translates the '~' symbol).
You should check errors.
I know that system calls are bad practice
Funny.
man 2 mkdir
Yes, sorry, it should be test instead of root. How would I use getenv ("HOME"); in an ofstream statement?
Nevermind. I have solved this problem by doing the following:
1
2
3
4
string homedir=getenv("HOME");
string filepath="a/test/test.txt";
homedir=homedir+filepath;
frankiscool.open(homedir.c_str());

Thanks.
Topic archived. No new replies allowed.