在linux 实现文件cp

本文介绍了一个简单的Linux环境下文件复制程序实现方法。该程序通过使用标准C库中的函数如open、read、write等来完成从一个文件到另一个文件的数据复制过程,并展示了完整的源代码。
 1 /*************************************
 2 函数功能:在linux 实现文件cp
 3 作者:张建起
 4 时间:2016.01.15
 5 *************************************/
 6 #include<stdio.h>
 7 #include<unistd.h>
 8 #include<stdlib.h>
 9 #include<pthread.h>
10 #include<semaphore.h>
11 #include<string.h>
12 #include<sys/types.h>
13 #include<sys/shm.h>
14 #include<sys/ipc.h>
15 #include<sys/sem.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 /************************************************/
19 struct FIEL                //任务节点
20 {
21     const char *r;
22     const char *w;
23     struct task  *next;        //节点指针
24 };
25 
26 int macp(struct FIEL *q)
27 {
28     char buf[102400];
29     int fd=open(q->r,O_RDONLY);                                //打开读文件
30     int fd1=open(q->w,O_RDWR|O_CREAT|O_APPEND, 0667);
31     ssize_t cnt;
32     ssize_t cnt1;
33     if(fd<0)                                                    //判断文件是否正确打开
34     {
35         perror("open pathname\n");
36         return -1;
37         }
38     if(fd1<0)                                                    //判断文件是否正确打开
39         {
40             perror("open pathname1\n");
41             return -1;
42         }
43         lseek(fd1,0,SEEK_SET);
44         do{
45         bzero(buf,sizeof(buf));
46         cnt=read(fd,buf,sizeof(buf));                            //读出数据
47         if(cnt<0)
48         {
49             perror("read pathname\n");
50             return -1;            
51         }
52         printf("1--------------\n");
53         cnt1=write(fd1,buf,cnt);
54         if(cnt1<0)            //写入数据
55         {
56             perror("write pathname\n");
57             return -1;                    
58         }
59         printf("2--------------\n");
60     }while(cnt==sizeof(buf));
61     close(fd);
62     close(fd1);    
63 }
64 /*****************主函数*****************/
65 int main()
66 {
67     const char *pathname="./open.c";        //读文件路径
68     const char *pathname1="./open1.c";    //写文件路径
69     struct FIEL *p=malloc(sizeof(struct FIEL));    
70     p->r=pathname;
71     p->w=pathname1;
72     macp(p);
73     return 0;
74 }

 

转载于:https://www.cnblogs.com/jikexianfeng/articles/5713708.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值