• C 判断路径存在


    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;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    int main()
    {
      struct stat buf;
     
      stat("////etc",&buf);
     
      if (S_ISDIR(buf.st_mode) == 0)
          {
              printf(" /etc is not directory ! ");
              //mkdir("/etc/123", 0755);
          }
      else
        printf(" /etc is directory ! ");
     
      return 0;
    }

    #define FILENAME ***.****
    BOOL Check(char * filename)
    {
      if(access(filename,0) == 0)
      return true;
      else
      return false;
    }
    int main()
    {
      Check(FILENAME );
    }
    pasting
  • 相关阅读:
    在Windows环境下使用docker
    C# 8.0中的模式匹配
    C# 8.0的新的using语法——Using declarations
    任务调度框架FluentScheduler简介
    任务调度框架Hangfire 简介
    Linux下的sqlserver简单试用
    使用Puppeteer进行数据抓取(五)——快速调试
    使用ptrace向已运行进程中注入.so并执行相关函数(转)
    ARM汇编编程基础之一 —— 寄存器
    ARM 汇编与C调用的若干问题(一般函数调用情况)
  • 原文地址:https://www.cnblogs.com/wxmdevelop/p/4645119.html
Copyright © 2020-2023  润新知