#include <stdio.h>
#include <string.h>
#include
<unistd.h>
//access()
#include
<sys/stat.h>
//创建多级目录
int createMultiLevelDir(char*
sPathName)
{
char
DirName[256];
int i,
len;
strcpy(DirName,
sPathName);
len =
strlen(DirName);
if(‘/‘ !=
DirName[len-1]) {
strcat(DirName, "/");
len++;
}
for(i=1; i<len;
i++)
{
if(‘/‘ == DirName[i])
{
DirName[i] = ‘\0‘;
if(access(DirName, F_OK) != 0)
{
if(mkdir(DirName, 0777) == -1)
{
perror("mkdir() failed!");
return -1;
}
}
DirName[i] = ‘/‘;
}
}
return 0;
}
int main()
{
char multi_level_dir[256] =
"/home/w/ww/www/wwww";
if(-1 == createMultiLevelDir(multi_level_dir)) {
perror("CreateMultiLevelDir() failed!");
return
-1;
}
printf("directroy \"%s\"
created sucessfully!\n", multi_level_dir);
return
0;
}
/*
当把源文件保存为createMultiLevelDir.cpp用gcc编译时出现__gxx_personality_v0编译错误:
是由于源代码的文件后缀名和代码的语法不相符合"比如用C的语法的文件,选择了cpp这样的后缀名,而且还用gcc而不是g++编译“。
*/