Sign in to follow this  

Creating a file, when the parent directories might not exist

This topic is 4663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Suppose I want to creat a file c:\x\y\z.txt the directories x and y do not exist. Is there a function i can call in win32 that will create the directories for me if they dont exist? Or will I have to manually parse the file path to obtain the parent directories of the file and create them each. Parsing strings in C/C++ isnt exactly fun, but none of the create fle functions seem to create the directories for you automatically.

Share this post


Link to post
Share on other sites
There's no function to do it that I know of, I just ended up writing my own function. I'll look for it now, but I can't remember what damn project it was in...

Share this post


Link to post
Share on other sites
Gottit:

FILE* CreateFileAndPath(const string& strPathAndFile)
{
string strPath(strPathAndFile);
string strCurrent;
int nStart, nStart1, nStart2;

// Create the path //
while(strPath.length())
{
nStart1 = (int)strPath.find('/'); if(nStart1 == string::npos) nStart1 = INT_MAX;
nStart2 = (int)strPath.find('\\'); if(nStart2 == string::npos) nStart2 = INT_MAX;
nStart = min(nStart1,nStart2);
if(nStart == INT_MAX) break;
strCurrent += strPath.substr(0,nStart)+"\\";
strPath.erase(strPath.begin(),strPath.begin()+nStart+1);
CreateDirectory(strCurrent.c_str(),NULL);
}

// Create the file //
return fopen(strPathAndFile.c_str(),"wb");
}




If you don't like std::string, you'll have to convert it yourself I'm afraid :P

The algorithm is pretty simple. Just keep searching for the next \ or / character, and create that directory. It'll try to create C:, and that'll fail, but that doesn't matter, it'll continue and create C:\x then C:\x\y then it'll do an fopen("C:\x\y\z.txt","wb").

Share this post


Link to post
Share on other sites
another thing you can do...
did this from my head so there may be errors :).


// split the path into its components
char drive[MAX_PATH];
char dir[MAX_PATH];
char fname[MAX_PATH];
char ext[MAX_PATH];
_splitpath(filename, drive, dir, fname, ext);

// SHCreateDirectoryEx creates all intermediate directories
string path = string(drive) + string(dir);
SHCreateDirectoryEx(0, path.c_str(), 0);

// create file
string fullpath = path + string(fname) + string(ext);
ofstream file(fullpath.c_str());

Share this post


Link to post
Share on other sites
You could let the boost library do most of the work for you:


// create a file, plus any required directories
std::ofstream CreateFileAndRequiredDirectories(const std::string& filePath)
{
// just to simplify things...
namespace filesystem = boost::filesystem;
using boost::filesystem::path;

// create a boost path object representing the file path;
path filePathObject(filePath, filesystem::native);
if(!filesystem::exists(filePathObject.branch()))
{
// if the file's parent directory doesn't exist,
// then we need to do a little more work...

// create a path object to represent the file's parent directory
path dirPath(filePathObject.branch(), filesystem::native);

// ...walk through each directory in the parent directory path
// from root to absolute path and create any parent directories
// that don't exist yet
path partialPath("", filesystem::native);
path::iterator currPath = dirPath.begin();
path::iterator endPath = dirPath.end();
while(currPath != endPath)
{
partialPath /= path(*currPath, filesystem::native);
if(!filesystem::exists(partialPath))
{ filesystem::create_directory(partialPath);
}
++currPath;
}
}
return std::ofstream(filePath);
}

Share this post


Link to post
Share on other sites
BOOL MakeSureDirectoryPathExists(PCSTR DirPath):
http://msdn.microsoft.com/library/en-us/debug/base/makesuredirectorypathexists.asp

Share this post


Link to post
Share on other sites
Quote:
Original post by Nis
BOOL MakeSureDirectoryPathExists(PCSTR DirPath):
http://msdn.microsoft.com/library/en-us/debug/base/makesuredirectorypathexists.asp
D'oh! I knew there'd be something simple like this...

Share this post


Link to post
Share on other sites

This topic is 4663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this