VC中如何创建多层目录

例如

gcreatedir("c:\\dir1\\dir2\\dir3");

如果只需要创建一层目录用 createdirectory 就可以了。
// create a directory.
// this function can create a directory under an unexist directory
// but createdirectory() api cant.
bool gcreatedir(cstring csdir)
{
handle ffile; // file handle
win32_find_data fileinfo; // file information structure
cstringarray m_arr; // cstring array to hold directory structures
bool tt; // bool used to test if create directory was successful
int x1 = 0; // counter
cstring tem = ""; // temporary cstring object

// before we go to a lot of work.
// does the file exist

ffile = findfirstfile(csdir,&fileinfo);

// if the file exists and it is a directory
if(fileinfo.dwfileattributes == file_attribute_directory)
{
// directory exists close file and return
findclose(ffile);
return true;
}

m_arr.removeall(); // not really necessary - just habit
for(x1=0;x1<csdir.getlength();x1++) // parse the supplied cstring directory string
{
if(csdir.getat(x1) != \\) // if the charachter is not a \
tem += csdir.getat(x1); // else add character to temp string
else
{
m_arr.add(tem); // if the character is a \ add the temp string to the cstring array
tem += "\\"; // now add the \ to the temp string
}
if(x1 == csdir.getlength()-1) // if we reached the end of the file add the remaining string
m_arr.add(tem);
}


// close the file
findclose(ffile);

// now lets cycle through the string array and create each directory in turn
for(x1 = 1;x1<m_arr.getsize();x1++)
{
tem = m_arr.getat(x1);
tt = createdirectory(tem,null);

// if the directory exists it will return a false
if(tt)
setfileattributes(tem,file_attribute_normal);
// if we were successful we set the attributes to normal
}
m_arr.removeall();
// now lets see if the directory was successfully created
ffile = findfirstfile(csdir,&fileinfo);

// if the file exists and it is a directory
if(fileinfo.dwfileattributes == file_attribute_directory)
{
// directory exists close file and return
findclose(ffile);
return true;
}
else
{
findclose(ffile);
return false;
}

}
原文地址:https://www.cnblogs.com/luoyaoquan/p/2034406.html