LINUX用C建立多级目录(测试通过)

阅读 65

2022-01-28


  • 方法一

调用system()执行mkdir -p命令。

  • 方法二

  具体代码如下:

int mkdirs(char* pPath, int isLastFile)
{
char buffer[BUFFER_SIZE] = {0};
char* next = pPath;
int isLast = 0;
while (next != NULL)
{
next = strchr(next+1, '/');
if (next == NULL)
{
if (isLastFile)
{
break;
}
isLast = 1;
next = pPath+strlen(pPath);
}
strncpy(buffer, pPath, next-pPath);
// DEBUG_TEXT(buffer);

if ( access(buffer, F_OK) != 0
&& mkdir(buffer, 0755) == -1)
{
return -1;
}

if (isLast)
{
break;
}
}
return 0;
}

strncpy可以节省一点,意义不大。


精彩评论(0)

0 0 举报