To create a directory, you can use the C function:
int mkdir(const char *pathname, mode_t mode);
If you can use Boost then it will become easier and more C ++ friendly:
bool create_directories(const path& p);
As you mentioned in your question, you can use both absolute and relative paths. It depends on your intentions. In your case, you can simply do:
boost::filesystem::create_directories("./BobtheBuilder"); ofstream out("./BobtheBuilder/NewFile.txt");
no need to specify an absolute path at all.
If you often need to manage your paths, Boost provides many useful traffic management tools. As an example, consider the problem that you mentioned in your question: you want to get the full path to the current directory, and then add the relative path. You can do this very easily:
#include <boost/filesystem.hpp> namespace fs = boost::filesystem; ... fs::path curr_abs_path = fs::current_path(); fs::path rel_path = "foo/bar"; fs::path combined = (curr_abs_path /= rel_path); cout << combined << endl;
Assuming the current directory is / tmp /, the previous code snippet will print:
/ Tmp / foo / bar
operator/=
is responsible for adding two paths and returning a combined result.
source share