指引网

当前位置: 主页 > 编程开发 > C >

C语言判断目录是否存在的例子

来源:网络 作者:佚名 点击: 时间:2017-07-19 23:06
[摘要]  要判断目录存在这个问题对于php或asp非常的简单一行代码搞定,但对于c语言来讲相对比较复杂了,下面看个例子。

例子

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc,char *argv[])
{
        struct stat filestat;
        if(argc!=2)
        {
                printf("Usage ./a.out argv1\n");
                exit(-1);
        }
        if(stat(argv[1],&filestat)!=0)
        {
                perror(argv[1]);
                return -1;
        }
        if(S_ISDIR(filestat.st_mode))
                printf("%s is dir \n",argv[1]);
        else
                printf("%s is not dir\n",argv[1]);
}

------分隔线----------------------------