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("\n\t/etc is not directory !\n\n");
          //mkdir("/etc/123", 0755);
      }
  else
    printf("\n\t/etc is directory !\n\n");
 
  return 0;
}

#define FILENAME ***.****
BOOL Check(char * filename)
{
  if(access(filename,0) == 0)
  return true;
  else
  return false;
}
int main()
{
  Check(FILENAME );
}
pasting

C 判断路径存在的更多相关文章

  1. shell 判断路径

    判断路径 ];then echo "找到了123" if [ -d /root/Desktop/text ] then echo "找到了text" else ...

  2. C# 判断路径和文件存在

    1.判断路径是否存在,不存在则创建路径: if (!System.IO.Directory.Exists(@"D:\Export")) { System.IO.Directory. ...

  3. git 判断路径是否是 git 仓库

    git 判断路径是否是 git 仓库 import subprocess repo_dir = "../path/to/check/" command = ['git', 'rev ...

  4. C# 判断路径是否存在

    定义文件状态枚举:0-路径为空,1-存在文件,2-路径不为空,但文件不存在 public enum FileExsitStatus { NoPath=0, FileExsit=1, NoFile=2 ...

  5. Python --判断路径是否为目录或文件

    os.path.isdir( ), os.path.isfile(),os.listdir( ), os.walk( ) 参考网址:https://blog.csdn.net/xxn_723911/a ...

  6. PAT甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789799.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  7. hdu 4096 判断路径

    思路:将每个关系当成一条有向边,查询时就判断之间存在路径. #include<iostream> #include<cstdio> #include<cstring> ...

  8. python 练习 simple_server 判断路径及返回函数

    函数 routers 返回一个 urlpatterns 元组,里面包含了路径名和函数名:在 函数 application 中遍历 urlpatterns 元组,路径存在则返回函数名,不存在则返回 40 ...

  9. python 判断路径是否存在

    import os os.path.exists(文件绝对路径)

随机推荐

  1. POJ1236 - Network of Schools tarjan

                                                     Network of Schools Time Limit: 1000MS   Memory Limi ...

  2. 国家与城市的sql

    --省表 create table tb_province ( pID int NOT NULL PRIMARY KEY, pName ) ) --省 ,'北京市') ,'天津市') ,'上海市') ...

  3. 前端JSON使用总结

    JSON: JavaScript Object Notation(JavaScript 对象表示法)的简称. 1. 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaS ...

  4. WCF 采用net.tcp协议实践

    概述 与Socket相比,WCF真是爽得不得了,其基本指导思想为SOA——面向服务. 其基本配置在于ABC(Address,Binding,Contract),通常,只要这三个因素配置对了,那么,基本 ...

  5. SGU438 The Glorious Karlutka River =)(最大流)

    题目大概说有m个人要过一条宽W的河,人最远跳远距离是d,河上有n个垃圾堆,每个垃圾堆都有坐标和同一时间能容纳的人数,问所有人最少要跳几次才能跳到对岸. 又是一题根据时间拆点的最大流. 二分时间建容量网 ...

  6. TYVJ P1029 牛棚回声 Label:坑

    背景 USACO OCT09 3RD 描述 奶牛们灰常享受在牛栏中牟叫,因為她们可以听到她们牟声的回音.虽然有时候并不能完全听到完整的回音.Bessie曾经是一个出色的秘书,所以她精确地纪录了所有的牟 ...

  7. 【POJ】1269 Intersecting Lines(计算几何基础)

    http://poj.org/problem?id=1269 我会说这种水题我手推公式+码代码用了1.5h? 还好新的一年里1A了---- #include <cstdio> #inclu ...

  8. 使用RBTool自动提交code review请求

    使用RBTool自动提交code review请求 前言 让我们回想一下手工提交review请求的过程: 首先得用 svn diff > filename.diff 生成diff文件. 然后输入 ...

  9. 下载站中的下载连接其实是php脚本文件控制

    什么是php文件,PHP是一种服务器端HTML-嵌入式脚本描述语言. 其最强大和最重要的特征是其数据库集成层,使用它完成一个含有数据库功能的网页是不可置信的简单.在HTML文件中, PHP脚本程序(语 ...

  10. Error 2147943712 during task creation

    In a Windows 2008 server, when you confirm the creation of a new task, you may obtain the following ...