gpt4 book ai didi

c - 在 unix 系统上确定符号链接(symbolic link)

转载 作者:太空宇宙 更新时间:2023-11-04 01:35:39 24 4
gpt4 key购买 nike

我正在尝试使用函数 S_ISLNK(file.st_mode) 来检查某个文件是符号链接(symbolic link)还是目录或常规文件。但是,当我使用符号链接(symbolic link)文件检查它时,它似乎不起作用。

这是我的代码:

            if(S_ISDIR(fileStat.st_mode))
{
// DIR - display files in the directory
printf(" DIR ");
fileType = 2;


}else if(S_ISLNK(fileStat.st_mode)){
// LNK - display the name of the file the link is pointing to
printf(" LNK ");
fileType = 3;
}else{
// Display general info only
printf(" REG ");
fileType = 1;
}

目录检查工作正常,但是当我在符号链接(symbolic link)文件上运行我的程序时,它显示为常规文件。有人知道我可能做错了什么吗?

最佳答案

stat 统计链接的目标。如果要判断文件是否为符号链接(symbolic link),请使用 lstat:

lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.

关于c - 在 unix 系统上确定符号链接(symbolic link),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14965217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com