判断目录是否存在并创建mkdir

Linux下mkdir函数
头文件库:

 
#include <sys/stat.h>
 
#include <sys/types.h>
 

  函数原型:

 
int mkdir(const char *pathname, mode_t mode);
 
函数说明:
 
mkdir()函数以mode方式创建一个以参数pathname命名的目录,mode定义新创建目录的权限。
 
返回值:
 
若目录创建成功,则返回0;否则返回-1,并将错误记录到全局变量errno中。
 
mode方式:
 
S_IRWXU00700权限,代表该文件所有者拥有读,写和执行操作的权限
S_IRUSR(S_IREAD)00400权限,代表该文件所有者拥有可读的权限
S_IWUSR(S_IWRITE)00200权限,代表该文件所有者拥有可写的权限
S_IXUSR(S_IEXEC)00100权限,代表该文件所有者拥有执行的权限
S_IRWXG00070权限,代表该文件用户组拥有读,写和执行操作的权限
S_IRGRP00040权限,代表该文件用户组拥有可读的权限
S_IWGRP00020权限,代表该文件用户组拥有可写的权限
S_IXGRP00010权限,代表该文件用户组拥有执行的权限
S_IRWXO00007权限,代表其他用户拥有读,写和执行操作的权限
S_IROTH00004权限,代表其他用户拥有可读的权限
S_IWOTH00002权限,代表其他用户拥有可写的权限
S_IXOTH00001权限,代表其他用户拥有执行的权限


1   用   int   access(const   char   *pathname,   int   mode);   判断有没有此文件或目录 --它区别不出这是文件还是目录
2   用   int   stat(const   char   *file_name,   struct   stat   *buf); 判断该文件或目录是否否存在 ;得到st_mode,然后判断是不是目录文件。 
    stat()系统调用看是否成功,不成功就不存在,成功判断返回的st_mode是否是一个文件夹。

********************************************************************
linux c关于目录是否存在,新建目录等操作
1. 创建目录

       #include <sys/stat.h>
       #include <sys/types.h>

       int mkdir(const char *pathname, mode_t mode);

运用条件:只能在已存在的目录下建立一级子目录

返回值:  返回0表示成功,返回-1表述出错。

mode 表示新目录的权限,可以取以下值:

其中,mode就用0777,0755这种形式。

 
2. 判断一个目录是否存在

可以使用opendir来判断,这是比较简单的办法。

       #include <sys/types.h>
       #include <dirent.h>

       DIR *opendir(const char *name);

       The  opendir()  function  opens  a  directory  stream  corresponding to the directory name, and returns a pointer to the directory

stream.  The stream is positioned at the first entry in the directory.

 

代码
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <cstddef>
int main()
{
 if(NULL==opendir("/d1/liujian/readdb/adTest/data/html"))
   mkdir("/d1/liujian/readdb/adTest/data/html",0775);
 return 0;
}

 

以上代码可以测试一个目录是否存在,如果不存在就创建这个目录。

***********************************

#include<stdio.h>
#include<string.h>
#include<errno.h>

#include<unistd.h>

#include<dirent.h>
#include<sys/types.h>
#include<sys/stat.h>

extern int errno;

#define MODE (S_IRWXU | S_IRWXG | S_IRWXO)

int mk_dir(char *dir)
{
    DIR *mydir = NULL;
    if((mydir= opendir(dir))==NULL)//判断目录 
    {
      int ret = mkdir(dir, MODE);//创建目录
      if (ret != 0)
      {
          return -1;
      }
      printf("%s created sucess!/n", dir);
    }
    else
    {
        printf("%s exist!/n", dir);
    }

    return 0;
}

int mk_all_dir(char *dir)
{
    bool flag = true;
    char *pDir = dir;
    while (flag)
    {
        char *pIndex = index(pDir, '/');
        if (pIndex != NULL && pIndex != dir)
        {
            char buffer[512] = {0};
            int msg_size = pIndex - dir;
            memcpy(buffer, dir, msg_size);
            int ret = mk_dir(buffer);
            if (ret < 0)
            {
                printf("%s created failed!/n", dir);
            }
        }
        else if (pIndex == NULL && pDir == dir)
        {
            printf("dir is not directory!/n");
            return -1;
        }
        else if (pIndex == NULL && pDir != dir)
        {
            int ret = mk_dir(dir);
            if (ret < 0)
            {
                printf("%s created failed!/n", dir);
            }

            break;
        }

        pDir = pIndex+1;

    }

    return 0;
}

 

int main()
{
    char buffer[512] = {0};
    printf("please input path mane/n");
    fgets(buffer, sizeof(buffer), stdin);
    
    char *pIndex = index(buffer, '/n');
    if (pIndex != NULL)
    {
        *pIndex = '/0';
    }

    printf("check path mane %s/n", buffer);

    int ret = mk_all_dir(buffer);
    if (ret < 0)
    {
        printf("% mkdir failed!/n", buffer);
        return -1;
    }

    return 0;
}


  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在Python中,可以使用`os`模块来检查目录是否存在创建目录。可以在`for`循环内使用以下代码: ```python import os for directory_name in directory_names: if not os.path.exists(directory_name): os.makedirs(directory_name) ``` 在上面的代码中,`directory_names`是一个包含目录名称的列表。循环遍历这个列表中的每个目录名称,然后使用`os.path.exists`函数检查该目录是否已经存在。如果目录不存在,则使用`os.makedirs`函数创建目录及其所有父级目录。 请注意,如果您计划创建多个目录,最好使用`os.makedirs`而不是`os.mkdir`。因为`os.makedirs`会自动创建整个目录树,而`os.mkdir`则只会创建一个目录,如果父级目录不存在,会导致出错。 ### 回答2: 可以使用Python的os模块来判断目录是否存在创建目录。具体实现如下: ```python import os path = "目录的路径" # 替换为你想要判断目录路径 if not os.path.exists(path): # 判断目录是否存在 os.makedirs(path) # 创建目录 print("目录创建") else: print("目录已存在") ``` 首先,导入os模块。然后,设置变量path为待判断目录路径。 在判断目录是否存在的条件语句中,使用`os.path.exists()`函数来判断目录是否存在。如果目录不存在,则调用`os.makedirs()`函数来创建目录,并输出"目录创建"。如果目录已存在,则直接输出"目录已存在"。 以上代码中,替换`"目录的路径"`为你要判断目录的实际路径,执行该代码即可判断目录是否存在并进行创建。 ### 回答3: 在进行循环判断目录是否存在创建目录的操作时,可以使用以下代码: ```python import os def create_directory(directory): if not os.path.exists(directory): # 判断目录是否存在 os.makedirs(directory) # 创建目录 for i in range(10): directory = "目录名{}".format(i) # 设置目录名 create_directory(directory) # 调用函数判断目录是否存在创建目录 ``` 这段代码首先导入了`os`模块,然后定义了一个函数`create_directory(directory)`。在函数体内,通过`os.path.exists(directory)`判断目录是否存在,如果不存在则使用`os.makedirs(directory)`创建目录。 接下来,在一个循环中,我们可以通过设置不同的目录名(例如"目录名0"、"目录名1"等等)来创建多个目录。循环会进行10次,每次都会调用`create_directory(directory)`函数来判断目录是否存在创建目录。 需要注意的是,如果你需要在目录名中使用其他的变量或者不同的命名方式,可以根据具体的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值