#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