C中如何判断一个文件是链接文件?

forwxh 2010-10-18 04:25:14
C中如何判断一个文件是链接文件?
谢谢:

使用S_ISLNK是否可以?
...全文
1206 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
justkk 2010-10-19
  • 打赏
  • 举报
回复
你不能使用fstat来获取文件信息,这样获取的是链接的目标文件
像1楼说的,可以使用lstat获取链接文件本身的信息,然后再测试
forwxh 2010-10-19
  • 打赏
  • 举报
回复
int main(int argc, char **argv)
{
if(argc != 2)
return 1;

int file=0;
if((file=open(argv[1],O_RDONLY)) < -1)
return 1;

struct stat fileStat;
if(fstat(file,&fileStat) < 0)
return 1;

printf("Information for %s\n",argv[1]);
printf("---------------------------\n");
printf("File Size: \t\t%d bytes\n",fileStat.st_size);
printf("Number of Links: \t%d\n",fileStat.st_nlink);
printf("File inode: \t\t%d\n",fileStat.st_ino);

printf("File Permissions: \t");
printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");

printf("The file %s a symbolic link\n\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");

return 0;
}

当测试一个链接文件123(指向/tmp/99999.txt)时,显示的是/tmp/99999.txt的属性,请指点下~~
sfd1234 2010-10-19
  • 打赏
  • 举报
回复
试了不行那就是不行嘛!
forwxh 2010-10-19
  • 打赏
  • 举报
回复
使用S_ISLNK时,当测试一个链接文件时,提示为不是链接文件:
struct stat filestat;
(s_link(filestat.st_mode))?"is":"is not"
yueyinggufan 2010-10-18
  • 打赏
  • 举报
回复
man fcntl
luciferisnotsatan 2010-10-18
  • 打赏
  • 举报
回复
lz你自己试下你的想法就可以了
justkk 2010-10-18
  • 打赏
  • 举报
回复
lz自己都想到了
why not try 一下..
小魔菇 2010-10-18
  • 打赏
  • 举报
回复
fstat- get file status
S_ISLNK(m) symbolic link?
try325 2010-10-18
  • 打赏
  • 举报
回复 1
是linux中的链接文件吗?
试试lstat()函数

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧