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 }