读书人

为一个文件建立一个符号链接打开这个

发布时间: 2012-02-08 19:52:21 作者: rapoo

为一个文件建立一个符号链接,打开这个符号链接文件,将该文件的内容读出,并复制它。
在LINUX下编写如题目所述的一个程序。。。我是LINUX菜鸟。。。希望高手指点下~~貌似要用到open和create等函数。。。求高手给一段源代码~

[解决办法]
杂终端执行,
./a.out oldpath newpath output//第一个是原文件名,第二个是符号连接名,第三个是复制后的输出文件名
运行后output文件权限可能会不能读取,需要chmod来修改

C/C++ code
 1 #include<stdio.h>  2 #include<unistd.h>  3 #include<sys/types.h>  4 #include<sys/stat.h>  5 #include<fcntl.h>  6 #include<string.h>  7 #include<stdlib.h>  8   9 int main(int argc,char **argv){ 10  11     char *oldpath = argv[1]; 12     char *newpath = argv[2]; 13     char *output = argv[3]; 14     char buf[1024]; 15     int n; 16  17     if(symlink(oldpath,newpath) < 0){ 18         perror("symlink");exit(-1); 19     } 20  21     int fdin = open(newpath,O_RDONLY); 22     int fdout = open(output,O_WRONLY|O_CREAT); 23     if(fdin < 0 || fdout < 0){ 24         perror("open");exit(-1); 25     } 26     while((n = read(fdin,buf,sizeof(buf))) > 0){ 27         write(fdout,buf,n); 28         bzero(buf,sizeof(buf)); 29     } 30     return 0; 31  32 }~ 

读书人网 >UNIXLINUX

热点推荐