C:检查文件的类型。使用lstat()和宏不会';行不通

C:检查文件的类型。使用lstat()和宏不会';行不通,c,file-type,stat,readdir,opendir,C,File Type,Stat,Readdir,Opendir,我使用opendir()打开一个目录,然后使用readdir()和lstat()获取该目录中每个文件的统计信息。在此之后,我编写了一段代码,在这段代码下,不会像我所想的那样工作。它确实列出了当前目录中的所有文件,但无论文件是常规文件、符号链接还是目录,它都不会打印出来 #include <sys/stat.h> #include <sys/types.h> #include <dirent.h> #include <stdio.h> void m

我使用opendir()打开一个目录,然后使用readdir()和lstat()获取该目录中每个文件的统计信息。在此之后,我编写了一段代码,在这段代码下,不会像我所想的那样工作。它确实列出了当前目录中的所有文件,但无论文件是常规文件、符号链接还是目录,它都不会打印出来

#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>

void main(){

    char* folder=".";                                     //folder to open

    DIR* dir_p;
    struct dirent* dir_element;
    struct stat file_info;

    // open directory
    dir_p=opendir(folder);

    // show some info for each file in given directory
    while(dir_element = readdir(dir_p)){

        lstat(dir_element->d_name, &file_info);          //getting a file stats

        puts(dir_element->d_name);                       // show current filename
        printf("file mode: %d\n", file_info.st_mode);

        // print what kind of file we are dealing with
        if (file_info.st_mode == S_IFDIR) puts("|| directory");
        if (file_info.st_mode == S_IFREG) puts("|| regular file");
        if (file_info.st_mode == S_IFLNK) puts("|| symbolic link");
    }

}
#包括
#包括
#包括
#包括
void main(){
char*folder=“.”;//要打开的文件夹
DIR*diru\p;
结构方向*方向元素;
结构统计文件信息;
//开放目录
dir\u p=opendir(文件夹);
//显示给定目录中每个文件的一些信息
while(dir_元素=readdir(dir_p)){
lstat(dir_元素->d_名称和文件信息);//获取文件状态
puts(dir_元素->d_名称);//显示当前文件名
printf(“文件模式:%d\n”,文件信息.st\u模式);
//打印我们正在处理的文件类型
如果(file_info.st_mode==S_IFDIR)放入(“| |目录”);
如果(file_info.st_mode==S_IFREG)放入(“| |常规文件”);
如果(file_info.st_mode==S_iflink)放入(“| |符号链接”);
}
}

模式包含大量信息

尝试以下类型的测试:

if (S_ISDIR(file_info.st_mode))  puts("|| directory");

有一组宏用于解释
st_模式
,这比您想象的要复杂。使用它们,而不是直接探测字段:

if (S_ISREG(file_info.st_mode))
    // file is a regular file
else if (S_ISLNK(file_info.st_mode))
    // ...

还有
s_ISDIR
s_ISSOCK
,还有更多。请参阅,例如,获取信息。

我知道这是几年后的事,但对于后代来说,你做得不对:
@alk是正确的。st_模式字段包含更多信息,例如文件类型、文件权限等
要提取文件类型,请在st_mode字段和文件类型掩码S_IFMT上执行按位and。然后检查结果是否符合要求。这就是厄内斯特·弗里德曼·希尔(Ernest Friedman Hill)提到的宏所做的。开关更适合进行全面检查,即

对于一个简单的情况:

     if ((file_info.st_mode & S_IFMT)==S_IFDIR) puts("|| directory");
要进行全面检查:

       struct stat st;
       ...

      switch (st.st_mode & S_IFMT) {
        case S_IFREG:  
            puts("|| regular file");
            break;
        case S_IFDIR:
            puts("|| directory");
            break;
        case S_IFCHR:        
            puts("|| character device");
            break;
        case S_IFBLK:        
            puts("|| block device");
            break;
        case S_IFLNK: 
            puts("|| symbolic link");
            break;
        case S_IFIFO: 
            puts("|| pipe");    
            break;
        case S_IFSOCK:
            puts("|| socket");
            break;
        default:
            puts("|| unknown"); 
     }

有人告诉过你main()应该返回int吗?这需要更多的支持。如果没有按位
S_,IFDIR
将无法工作。