通过例子来说明:
我要在当前目录下创建 head/follow/end/ 目录
C语言中mkdir原型为int mkdir(const char *pathname, mode_t mode);
mkdir()函数以mode方式创建一个以pathname为名字的目录,mode定义所创建目录的权限
返回值: 0:目录创建成功 -1:创建失败
mkdir只能一次创建一层目录
有两种实现方式
一:直接创建
- #include<sys/stat.h>
- #include<sys/types.h>
- mkdir(“head”,0777);
- mkdir(“head/follow”.0777);
- mkdir(“head/follow/end”,0777);
二:写函数
- void mkdirs(char *muldir)
- {
- int i,len;
- char str[512];
- strncpy(str, muldir, 512);
- len=strlen(str);
- for( i=0; i<len; i++ )
- {
- if( str[i]=='/' )
- {
- str[i] = '\0';
- if( access(str,0)!=0 )
- {
- mkdir( str, 0777 );
- }
- str[i]='/';
- }
- }
- if( len>0 && access(str,0)!=0 )
- {
- mkdir( str, 0777 );
- }
- return;
- }
之后调用
mkdirs(head/follow/end)
即可
</div>