现在的位置: 首页 > 综合 > 正文

C语言判断目录是否存在

2018年04月03日 ⁄ 综合 ⁄ 共 424字 ⁄ 字号 评论关闭

From: http://www.averainy.info/c_yu_yan_pan_duan_mu_lu_shi_fou_cun_zai_like-unix/

#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]);
}

抱歉!评论已关闭.