I'm sorry for asking about something well documented, but while I should be able to use an absolute path such as:
outFile = "~/myfile.txt";
or
outFile = "/myDir/myfile.txt";
followed by an
ofstream o;
o.open(outFile.c_str());
However, o.is_open() returns a false. It does return true for any absolute path. My workaround is simply "../../myfile.txt" but that isn't ideal.
This isn't permissions related because I can get at my target location using relative paths, just not absolute paths. Has anyone had any issues with this on unix (MacOS 10.7)?
Full disclosure, noob c++ programmer here, obviously.
An absolute path starts at the root, so the path will look more like outFile = "/home/yourusername/myDir/myfile.txt";
I don't think it will work using ~ in the path. If you want the path of your home directory you can read the HOME environment variable using std::getenv("HOME");.
Excellent. TIL about std::getenv(.). Thank you very much. This works.
FWIW, I had been trying the path from the root directory without success:
outFile = "/Users/me/myfile.txt";
If you "cd /" at a Unix prompt, you will see the Users folder and you can cd there with this path. However, getenv("HOME") returns "/Volumes/myVolume/Users/me". Setting this as the absolute path also works.