所需头文件

#include <sys/stat.h>

S_ISDIR()函数的作用是判断一个路径是不是目录
看下面demon之前,介绍下stat结构体

struct stat{
mode_t st_mode; //文件对应的模式,文件,目录等
ino_t st_ino; //i-node节点号
dev_t st_dev; //文件所在设备的ID
dev_t st_rdev; //特殊设备号码
nlink_t st_nlink; //文件的连接数
uid_t st_uid; //文件所有者
gid_t st_gid; //文件所有者对应的组
off_t st_size; //普通文件,对应的文件字节数
time_t st_atime; //文件最后被访问的时间
time_t st_mtime; //文件内容最后被修改的时间
time_t st_ctime; //文件状态(属性)改变时间
blksize_t st_blksize; //文件内容对应的块大小
blkcnt_t st_blocks; //文件内容对应的块数量
};
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


int main(int argc,const char *argv[])
{
	printf("argc is %d\n",argc);
	if(argc!=2)
	{
		printf("arg numver erro\n");
	}

	struct stat statbuf;
	int res = -1;
	res = lstat(argv[1], &statbuf);//获取linux操作系统下文件的属性
	//参数1是传入参数,填写文件的绝对路径 | 参数2是传出参数,一个struct stat类型的结构体指针

	if (0 != res) {
		printf("lstat failed %s",argv[1]);
		return -1;
	}

	if (S_ISDIR(statbuf.st_mode)) {
		printf("%s是目录====\n",argv[1]);
	}

	return 0;
}

运行结果
在这里插入图片描述

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐