s_isdir()
是一个函数,通常用于检查指定路径是否为一个目录。
具体而言,它可以用于判断指定的路径字符串是否表示一个存在且可读的目录。如果是,函数将返回一个非零值,否则返回零。
在使用此函数之前,需要确保在代码中包含相关的头文件和库,以便可以调用该函数。
以下是一个使用示例:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
if (argc < 2) {
printf("Usage: %s <directory>\n", argv[0]);
exit(EXIT_FAILURE);
const char *dir_path = argv[1];
struct stat dir_stat;
if (stat(dir_path, &dir_stat) != 0) {
perror("stat");
exit(EXIT_FAILURE);
if (S_ISDIR(dir_stat.st_mode)) {
printf("%s is a directory.\n", dir_path);
} else {
printf("%s is not a directory.\n", dir_path);
return 0;
该示例程序接收一个路径参数,并使用 stat 函数获取该路径所代表的文件的状态信息。然后,通过 S_ISDIR 宏判断该文件是否是一个目录,并打印相应的输出。
注意:在使用 s_isdir() 函数之前,需要通过 stat 函数获取指定路径所代表的文件的状态信息。因此,在示例程序中,我们首先调用了 stat 函数。