在Linux下用c语言 如何判断文件是文件还是目录

在Linux下用c语言 如何判断文件是文件还是目录?
例如输入 /root就是目录,输入 /root/test.txt就是文件。

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

int main(int argc,char *argv[])
{
struct stat st;
printf("%s",argv[1]);
stat(argv[1],&st);
if (S_ISDIR(st.st_mode))
printf("is a dir\n");
else
printf("is not a dir\n");
return 0;
}

虚拟机上测过了.
是验证输入的第一个参数是不是目录.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-11-28
有函数可以识别。具体是哪个就不知道了。
我只知道 php 是有的。
相似回答