Linux 使用stat结构体访问目录,分辨不了文件和目录

ouyixi 2018-10-31 10:42:31
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
#include<dirent.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char *argv[]){
DIR *dp;
struct dirent *dr;
struct stat buf;
char *s_path,*d_path;
s_path=argv[1];
d_path=argv[2];
dp=opendir(s_path);
while((dr=readdir(dp))!=NULL){
stat(dr->d_name,&buf);
puts(dr->d_name);
printf("%d\n",buf.st_mode);
if(S_ISDIR(buf.st_mode)/*&&strcmp(dr->d_name,".")&&strcmp(dr->d_name,"..")*/){
printf("this is a dir\n");
//puts(dr->d_name);
}
}
return 0;
}



输出结果 8进制输出 c和dir不应该能被识别出来吗?
这个是DIR 和 FILE 对应的st_mode
#define S_IFDIR 0040000
#define S_IFREG 0100000
...全文
88 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

18,773

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧