用于创建软链接文件。
1.头文件
#include <unistd.h>
2.函数原型
int symlink(const char *target, const char *linkpath);
3.参数
target:软链接将要指向的源文件。
linkpath:软链接的链接文件名称。
4.返回值
成功返回0,失败返回-1,并会设置errno。
2.7.2 link
用于创建硬链接文件。
1.头文件
#include <unistd.h>
2.函数原型
int link(const char *oldpath, const char *newpath);
3.参数
oldpath:硬链接的源文件。
newpath:硬链接的链接文件名称。
4.返回值
成功返回0,失败返回-1,并会设置errno。
5.示例:(使用symlink和link创建链接文件)
| #include <unistd.h>
#include <stdio.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdlib.h> int main(){ const char *source = "source.txt"; const char *soft = "soft.txt"; const char *hard = "hard.txt"; struct stat statbuf; printf("File name:%sn",source); if (stat(source, &statbuf) == -1) { if (errno == ENOENT) { fprintf(stderr,"File dies not exist:%sn",source); } else { perror("stat"); } return EXIT_FAILURE; } if (link(source, hard) == -1) { perror("link"); return 1; } printf("Hard link '%s' created successfully.n", hard); if (symlink(source, soft) == -1) { perror("symlink"); return 1; } printf("Symbolic link '%s' created successfully.n", soft); return 0; } |
查看执行效果:
| $ ./sour_soft
File name:source.txt Hard link 'hard.txt' created successfully. Symbolic link 'soft.txt' created successfully. |
查看文件详细信息:
|
从文件详细信息中可以看到soft.txt为软连接文件,hard.txt为硬连接文件。
170