Linux系统编程之软硬连接函数

本文介绍了如何使用C语言创建硬链接(link)、软连接(symlink),以及删除链接(unlink)的操作,并展示了读取符号链接(readlink)内容的方法。通过实例演示了在信息技术中管理文件链接的基本实践。
摘要由CSDN通过智能技术生成

1、创建硬链接
函数原型
int link(const charoldpath,const charnewpath);

  • oldpath:原文件
  • newpath:硬链接文件
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
  link("helllo", "hello.hard");
 return 0;
}

输出:
在这里插入图片描述
2、软连接

int symlink(const char*target,const char *linkpath)

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
  symlink("helllo", "hello.soft1");
 return 0;
}

输出:
在这里插入图片描述
3、删除符号链接或者硬链接计数

int unlink(const char*pathname);

注意:

  • 可以删掉软连接也可以删掉硬链接,文件也可以
  • 有其他进程使用该文件,unlink可以先不删,继续执行操作,在进程退出后文件删掉

4、读取符号链接本身内容,得到连接指向的文件名
函数原型:ssize_t readlink(const char*path,char *buf,size_t bufsiz);

  • pathname :链接名
  • buf:缓冲区
  • bufsiz:缓冲区大小
    成功:返回buf填充大小,失败返回-1
    注意:读取软连接,不能读取硬链接
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
  char buf[32]={0};
  readlink("hello.soft1",buf,sizeof(buf));
  printf("buf is %s\n",buf);
  return 0;
}

输出:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值