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. NDK编译生成so文件

    1 首先加载项目

  2. 利用SQLiteOpenHelper来管理SQLite数据库 (转)

    转载自 利用SQLiteOpenHelper来管理SQLite数据库 http://blog.csdn.net/conowen/article/details/7306545 Android学习笔记( ...

  3. hdu 4640(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4640 思路:f[i][j]表示一个人状态i下走到j的最小花费,dp[i][j]表示i个人在状态j下的最 ...

  4. JavaScript案例五:下拉列表左右选择

    用JavaScript实现下拉列表左右选择,很简单,不过要特别注意循环时要注意变量是否发生了变化(见代码) <!DOCTYPE html> <html> <head> ...

  5. 第五根k线

    无论是下落还是上涨的一波,到第五根k线就要注意了.要么加速,不然就要翻转了

  6. 枚举GC Roots的实现

    枚举根节点 从可达性分析中从GC Roots节点找引用链这个操作为例,可作为GC Roots的节点主要在全局性的引用(例如常量或类静态属性)与执行上下文(例如栈帧中的本地变量表)中,现在很多应用仅仅方 ...

  7. BZOJ4295 : [PA2015]Hazard

    第i轮,a[i%n]+=b[i%m]. 枚举i,计算它变为0的次数,假设为t,那么有t=i+kn. 对于所有的i和k,(i+kn)%m形成了若干个总长度为m的环. 对于每个a[i],先在环中求出一轮最 ...

  8. JavaScript事件流

    什么是JS事件流 早期的IE事件传播方向为由上至下,即从document逐级向下传播到目标元素:而Netscape公司的Netscape Navigator则是朝相反的方向传播, 也就是从目标元素开始 ...

  9. COJ979 WZJ的数据结构(负二十一)

    试题描述 请你实现一个数据结构,完成这样的功能: 给你一个N个点的图,初始状态无边. 每次加入一条双向边(u,v,w),若加入后没有构成一棵生成树,输出“Not Yet”,否则输出当前最小生成树的权值 ...

  10. SQL 标量函数-----日期函数datediff()、 day() 、month()、year()

    select day(createtime) from life_unite_product     --取时间字段的天值 select month(createtime) from life_uni ...