工作程序中较少使用c语言来操作目录,目录函数参数参考:
https://blog.csdn.net/qq_20916555/article/details/51132642
https://blog.csdn.net/wangqingchuan92/article/details/80109793
https://blog.csdn.net/qq_27870421/article/details/102775005
在这实现创建多级目录,linux代码如下
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include <io.h>
#include <stdio.h>
#define MAX_FILENAME_LENGTH 512
int mkdirs(char *muldir)
{
int iRetVal = 0;
char tempStr[MAX_FILENAME_LENGTH] = { 0 };
if (!muldir)
{
return iRetVal;
}
strncpy(tempStr, muldir, MAX_FILENAME_LENGTH);
for (int i = 1; i<strlen(tempStr); i++)
{
if (tempStr[i] == '/')
{
tempStr[i] = '\0';
if (access(tempStr, 0) != 0)
{
if (0 != mkdir(tempStr, 0777))
{
iRetVal = 0;
break;
}
}
tempStr[i] = '/';
}
}
if (strlen(tempStr)>0 && access(tempStr, 0) != 0)
{
if (0 != mkdir(tempStr, 0777))
{
iRetVal = 0;
}
}
iRetVal = 1;
return iRetVal;
}
int main()
{
mkdirs("/home/ssd/storPic/1/");
return 0;
}