C:< SYS / stat.h>功能S_ISLNK,S_ISDIR和S_ISREG奇怪的行为? [英] C: <sys/stat.h> functions S_ISLNK, S_ISDIR and S_ISREG behaving oddly?

查看:781
本文介绍了C:< SYS / stat.h>功能S_ISLNK,S_ISDIR和S_ISREG奇怪的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code这是取自编译罚款。它打印的文件名在目录中以字母在它前面的选项:要么是 D ˚F 0 取决于他们的文件类型( 0 其他)。不过,我测试了它在目录的/ etc /网络里面有所谓的符号文件运行,它表现为 D ?我试着重新编排的 if语句太秩序,而是给出了一个不令人满意的输出了。我使用它不正确?

 ,而((ENT = READDIR(DP))!= NULL){
    LSTAT(ent-> d_name,&安培; ST);
    如果(COL){
            如果(S_ISDIR(st.st_mode)){
                    的printf(D \\ t的);
                    }
           否则如果(S_ISREG(st.st_mode)){
                    的printf(F \\ t的);
                    }
            否则如果(S_ISLNK(st.st_mode)){
                    的printf(L \\ t的);
            }
            其他{
                     的printf(O \\ t的);
            }
    }


解决方案

这可能工作作为一种替代方案:

 如果(COL){            如果(ent-> d_type == DT_DIR)
                的printf(D);
            否则,如果(ent-> d_type == DT_LNK)
                的printf(L);
            否则,如果(ent-> d_type == DT_REG)
                的printf(F);
        }

The code this is taken from compiles fine. It prints file names in a directory with the option of a letter in front of it: either a d,f,l, or o depending on their file type (o for other). However, I tested it on the directory /etc/network which has a symbolic file called run and it appeared as d? I've tried re-arranging the order of the if-statements too, but that gives an unsatisfactory output too. Am I using it incorrectly?

while ((ent = readdir (dp)) != NULL) {
    lstat(ent->d_name, &st);
    if (col){
            if(S_ISDIR(st.st_mode)){
                    printf("d\t");
                    }
           else if (S_ISREG(st.st_mode)){
                    printf("f\t");
                    }
            else if (S_ISLNK(st.st_mode)){
                    printf("l\t");
            }
            else {   
                     printf("o\t");   
            }
    }

解决方案

This might work as an alternative solution:

if(col){

            if(ent->d_type == DT_DIR)
                printf("d ");
            else if(ent->d_type == DT_LNK)
                printf("l ");
            else if(ent->d_type == DT_REG)
                printf("f ");
        }

这篇关于C:< SYS / stat.h>功能S_ISLNK,S_ISDIR和S_ISREG奇怪的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆