c - 给定的文件参数是否是目录。 C

标签 c directory argv argc

我正在寻找一个安静的代码来检查我传递给我的程序的参数是否是一个目录。到目前为止,我发现了这个:

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

int main(int argc, char *argv[])
{
    struct stat buf;

    stat(argv[1],&buf);

    exit(0);

}

但它并没有真正帮助我。

最佳答案

使用:

if(S_ISDIR(buf.st_mode))
   printf(" Its a directoy\n");
else
   printf("Its a file\n");

stat(argv[1],&buf); 调用之后

关于c - 给定的文件参数是否是目录。 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19464571/

相关文章:

c - sigaction 处理程序不关闭进程

c - 如何处理 argv 字符数组赋值?

c++ - 使用指针打印 argv 和 environ

c - 如何在没有正则表达式的情况下用逗号(可选)后跟空格来拆分字符串?

c++ - 对从串口接收到的字符串进行分段

c++ - C/C++ 中的基本多线程——提示、建议、教程、一些方向?

recursion - 使用 Inno Setup PreProcessor 获取源路径及其子目录的文件和大小

python - 使用 Python 将文件夹上传到 Google Cloud Storage?

c# - 在 C# 中递归搜索目录

c - 在 C 中对 argv[1] 使用 atoi() 时出现算术溢出