Linux/Unix 判断一个路径是目录还是文件

来源:互联网 发布:淘宝店铺号和旺旺号 编辑:程序博客网 时间:2024/06/11 04:44
#include <stdio.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>/** * 判断路径是目录还是文件 */void pathType(const char* filename){    struct stat info;    int r=stat(filename, &info);    printf("%s  ", filename);    if(r==0)    {        if(S_ISDIR(info.st_mode)) printf("folder\n");        else printf("file\n");    }else    {        if( errno==ENOENT) printf("nonexistent\n");        else printf("error");    }}int main(){    pathType("/etcc"); // nonexistent    pathType("/etc/host");    // file    pathType("/etc");       // folder    return 0;}

结果如图所示:
这里写图片描述

struct stat详解链接。

0 0
原创粉丝点击