总体概述:在上一篇文章中介绍了如何实现软链接命令并创建软链接,在这一章我们介绍如何编写程序,实现删除链接文件程序的方法,需要用到lstat,unlink函数和S_ISLNK宏。函数语法:lstat函数
总体概述:
在上一篇文章中介绍了如何实现软链接命令并创建软链接,在这一章我们介绍如何编写程序,实现删除链接文件程序的方法,需要用到lstat,unlink函数和S_ISLNK宏。
函数语法:
lstat函数语法:int lstat(const char *pathname, struct stat *statbuf);
unlink函数语法:int unlink(const char *pathname);
函数作用:
lstat函数的作用:获取指定文件的信息。
unlink函数的作用:从文件系统中删除一个名称。
S_ISLNK宏:判断是否为链接文件
参数介绍:
lstat函数参数介绍:
- 输入参数:pathname => 文件路径名。
- 输出参数:buf => 文件信息缓存
- 返回值:成功:返回 0;失败:返回 -1。
unlink函数参数介绍:
- 输入参数:pathname => 文件路径名。
- 返回值:成功:返回 0;失败:返回 -1。
代码示例:
#include #include #include #include #include #include int main(int argc, char **argv){ if (argc != 2) { printf(\"usage: %s .\\n\", argv[0]); return -1; } struct stat st; const char *pathname = argv[1]; if (lstat(pathname, &st) == -1) { printf(\"get file %s information failed, %s.\\n\", \\ pathname, strerror(errno)); return -1; } if (!S_ISLNK(st.st_mode)) { printf(\"%s is not a symbolic link file.\\n\", pathname); return -1; } return unlink(pathname);}
使用方法:
$ mkdir ~/clanguage && cd ~/clanguage$ touch unlink.c 注:创建并拷贝代码到unlink.c,保存退出。$ gcc -o unlink unlink.c$ touch hello.txt$ ./symlink hello.txt world.txt$ ./unlink world.txt
运行效果:
运行效果图
本文内容由互联网用户自发贡献,该文观点仅代表作者本人,本站仅供展示。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 97552693@qq.com 举报,一经查实,本站将立刻删除。