如何在C中创建目录树? [英] How can I create directory tree in C?
本文介绍了如何在C中创建目录树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用一种简单的方法在C中创建多个目录。
I want an easy way to create multiple directories in C.
例如,我想在以下目录中创建目录:
For example I want to create directory in:
/ a / b / c
但是如果目录不存在,我希望它们成为自动创建。我该怎么做?
but if the directories are not there I want them to be created automagically. How can I do this ?
推荐答案
这是一个小的C程序,用于创建目录树 a / b / c
在当前目录中:
Here is a small C program to create the directory tree a/b/c
in the current directory:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
int create_dir(char *name)
{
int rc;
rc = mkdir(name, S_IRWXU);
if (rc != 0 && errno != EEXIST)
{
perror("mkdir");
exit(1);
}
if (rc != 0 && errno == EEXIST)
printf("%s already exists.\n", name);
return 0;
}
int main(int argc, char **argv)
{
create_dir("a");
create_dir("a/b");
create_dir("a/b/c");
exit(0);
}
这篇关于如何在C中创建目录树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文