C 查找文件是目录还是文件

C 查找文件是目录还是文件,c,C,我正在写一个基本程序,它接受一个目录的名称并打开它。一旦打开它,我想对文件和子目录进行分类。我发现哪个做了类似的事情-但这是我得到的结果: [.] -> [0] [..] -> [0] [a.txt] -> [0] [subDir] -> [0] 其中a.txt是文本填充,subDir是目录,但这两个值都返回0。我不知道我会错在哪里 完整代码: #include <stdlib.h> #include <stdio.h> #include <

我正在写一个基本程序,它接受一个目录的名称并打开它。一旦打开它,我想对文件和子目录进行分类。我发现哪个做了类似的事情-但这是我得到的结果:

[.] -> [0]
[..] -> [0]
[a.txt] -> [0]
[subDir] -> [0]
其中a.txt是文本填充,subDir是目录,但这两个值都返回0。我不知道我会错在哪里

完整代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <dirent.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

void handleDirectory(DIR *dir);
int isFile(const char *path);

int main(int argc, char **argv)
{
    if (argc != 2){
        printf("Not enough arguments!\n");
        return 0; 
    }

    DIR *dir = opendir(argv[1]);
    if (dir == NULL){
      return 0;
    }

    handleDirectory(dir);
} // end of main 

void handleDirectory(DIR *dir){
    struct dirent *temp;
    while ((temp = readdir(dir)) != NULL) {
        printf("[%s] -> [%d]\n", temp->d_name, isFile(temp->d_name));
    }
} // end of handle directory method 

int isFile(const char *path)
{
    struct stat path_stat;
    lstat(path, &path_stat);
    return S_ISREG(path_stat.st_mode); 
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
无效处理董事(DIR*DIR);
int isFile(常量字符*路径);
int main(int argc,字符**argv)
{
如果(argc!=2){
printf(“参数不足!\n”);
返回0;
}
DIR*DIR=opendir(argv[1]);
if(dir==NULL){
返回0;
}
执行董事(dir);
}//主管道末端
无效处理目录(目录*目录){
结构方向*温度;
而((temp=readdir(dir))!=NULL){
printf(“[%s]->[%d]\n”,temp->d_名称,isFile(temp->d_名称));
}
}//句柄结束目录方法
int isFile(常量字符*路径)
{
结构状态路径\ u状态;
lstat(路径和路径统计);
返回S_ISREG(路径统计st_模式);
}
正如塔德曼所建议的,(或fstat()或lstat())是查询文件类型的好选择。返回的“stat”结构中的st_模式告诉您文件类型和模式:S_IFREG=“常规文件”、S_IFDIR=“目录”,等等

/这当然是一个合理的选择。“dirent”结构中的“d_type”告诉您文件类型:DT_REG=“常规文件”,DT_DIR=“目录”,等等

如果调用fstat时出现编译错误,则需要1)向我们显示代码,2)复制/粘贴准确的错误文本

代码检查的唯一内容是“st_mode==S_ISREG”。至少,我会更新代码以检查sb.st_mode&S_IFMT,根据以下示例:

#包括
#包括
#包括
#包括
#包括
#包括
#包括
int
main(int argc,char*argv[])
{
结构统计某人;
如果(argc!=2){
fprintf(stderr,“用法:%s\n”,argv[0]);
退出(退出失败);
}
if(lstat(argv[1],&sb)=-1){
佩罗尔(“lstat”);
退出(退出失败);
}
printf(“包含设备的ID:[%jx,%jx]\n”,
(uintmax_t)少校(sb.st_dev),
(最高)小调(圣德);
printf(“文件类型:”);
开关(sb.st\U模式和S\U IFMT){
案例S_IFBLK:printf(“块设备”);中断;
案例S_IFCHR:printf(“字符设备”);中断;
案例S_IFDIR:printf(“目录”);break;
案例S_IFFO:printf(“FIFO/管道”);中断;
案例S_iFlink:printf(“符号链接”);中断;
案例S_IFREG:printf(“常规文件”);中断;
案例S_IFSOCK:printf(“套接字”);中断;
默认值:printf(“未知?\n”);中断;
}
printf(“I节点编号:%ju\n”,(uintmax\u t)sb.st\u ino);
printf(“模式:%jo(八进制)\n”,
(uintmax_t)sb.st_模式);
printf(“链接计数:%ju\n”,(uintmax\u t)sb.st\u nlink);
printf(“所有权:UID=%ju GID=%ju\n”,
(uintmax_t)sb.st_uid,(uintmax_t)sb.st_gid);
printf(“首选I/O块大小:%jd字节\n”,
(intmax_t)sb.st_blksize);
printf(“文件大小:%jd字节\n”,
(intmax_t)sb.st_尺寸);
printf(“分配的块:%jd\n”,
(intmax_t)sb.st_街区);
printf(“上次状态更改:%s”,ctime(&sb.st_-ctime));
printf(“上次文件访问:%s”,ctime(&sb.st_atime));
printf(“上次文件修改:%s”,ctime(&sb.st_mtime));
退出(退出成功);
}

通常您会使用
fstat
而不是
lstat
,除非您关心软链接的细节。如果您想知道它是否是一个目录,请测试vs.
s\u IFDIR
@tadman no,它不适用于任何其他文件。该文件只是一个txt文件和子目录该问题作为两个问题的副本关闭-我选择的是。@JonathanLeffler非常感谢-这真的很有帮助。但我只是对如何使用chdir()将我的工作目录更改为argv[1]感到困惑,或者在调用stat()之前将argv[1]前置到每个文件名。我没有收到任何编译错误-我尝试了lstat和stat,但都得到了相同的结果。您能澄清一下opendir()和readdir()的含义吗@paulsm4Q:你能澄清一下你对opendir()和readdir()的意思吗?答:您说您正在使用opendir(),然后使用readdir()对文件和子目录进行分类。好的每个返回一个DIR*“dirent”,您可以使用它来检查该条目是文件、目录还是其他条目。您不需要额外调用“lstat()”。看看我上面引用的链接,或者看看这些例子:
   #include <sys/types.h>
   #include <sys/stat.h>
   #include <stdint.h>
   #include <time.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <sys/sysmacros.h>

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

       if (argc != 2) {
           fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
           exit(EXIT_FAILURE);
       }

       if (lstat(argv[1], &sb) == -1) {
           perror("lstat");
           exit(EXIT_FAILURE);
       }

       printf("ID of containing device:  [%jx,%jx]\n",
               (uintmax_t) major(sb.st_dev),
               (uintmax_t) minor(sb.st_dev));

       printf("File type:                ");

       switch (sb.st_mode & S_IFMT) {
       case S_IFBLK:  printf("block device\n");            break;
       case S_IFCHR:  printf("character device\n");        break;
       case S_IFDIR:  printf("directory\n");               break;
       case S_IFIFO:  printf("FIFO/pipe\n");               break;
       case S_IFLNK:  printf("symlink\n");                 break;
       case S_IFREG:  printf("regular file\n");            break;
       case S_IFSOCK: printf("socket\n");                  break;
       default:       printf("unknown?\n");                break;
       }

       printf("I-node number:            %ju\n", (uintmax_t) sb.st_ino);

       printf("Mode:                     %jo (octal)\n",
               (uintmax_t) sb.st_mode);

       printf("Link count:               %ju\n", (uintmax_t) sb.st_nlink);
       printf("Ownership:                UID=%ju   GID=%ju\n",
               (uintmax_t) sb.st_uid, (uintmax_t) sb.st_gid);

       printf("Preferred I/O block size: %jd bytes\n",
               (intmax_t) sb.st_blksize);
       printf("File size:                %jd bytes\n",
               (intmax_t) sb.st_size);
       printf("Blocks allocated:         %jd\n",
               (intmax_t) sb.st_blocks);

       printf("Last status change:       %s", ctime(&sb.st_ctime));
       printf("Last file access:         %s", ctime(&sb.st_atime));
       printf("Last file modification:   %s", ctime(&sb.st_mtime));

       exit(EXIT_SUCCESS);
   }