C:&LT; SYS / stat.h&GT;功能S_ISLNK,S_ISDIR和S_ISREG奇怪的行为? [英] C: <sys/stat.h> functions S_ISLNK, S_ISDIR and S_ISREG behaving oddly?
问题描述
在code这是取自编译罚款。它打印的文件名在目录中以字母在它前面的选项:要么是 D
,˚F
, →
或 0
取决于他们的文件类型( 0
其他)。不过,我测试了它在目录的/ etc /网络
里面有所谓的符号文件运行
,它表现为 D
?我试着重新编排的 if语句
太秩序,而是给出了一个不令人满意的输出了。我使用它不正确?
,而((ENT = READDIR(DP))!= NULL){
LSTAT(ent-&GT; 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-&GT; d_type == DT_DIR)
的printf(D);
否则,如果(ent-&GT; d_type == DT_LNK)
的printf(L);
否则,如果(ent-&GT; 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:&LT; SYS / stat.h&GT;功能S_ISLNK,S_ISDIR和S_ISREG奇怪的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!