C 语言 查看文件,文件夹是否存在 access,opendir

int is_dir_exist(const char *dir_path)
{
    if(dir_path == NULL)
    {
        return -1;
    }
    if(opendir(dir_path) == NULL)
    {
        return -1;
    }
    return 0;
}

在头文件unistd.h中的预定义如下:
#define R_OK 4 /* Test for read permission. */
#define W_OK 2 /* Test for write permission. */
#define X_OK 1 /* Test for execute permission. */
#define F_OK 0 /* Test for existence. */
具体含义如下:
R_OK 只判断是否有读权限
W_OK 只判断是否有写权限
X_OK 判断是否有执行权限
F_OK 只判断是否存在

如果指定的存取方式有效,则函数返回0,否则函数返回-1。

if(access(retfile, F_OK) == -1)     
{               
    return NULL;
}
    if(access("/tmp/dhcp.leases.tmp", F_OK) == 0)   // file  exist
    {               
        system("sed -i \"1d\" /tmp/dhcp.leases.tmp"); // del "net_arp_array type IP HW type"
        system("sed -i '/eth1/d' /tmp/dhcp.leases.tmp");//del  Local WAN
    }

猜你喜欢

转载自blog.csdn.net/linbounconstraint/article/details/80250952