- #if 0
- 需要注意的问题:
- 1:使用open函数的时候一定要注意其属性方面的选择,
- 需要写入的文件一定包含O_WRONLY,否则写的时候会报错由于没有写权限
- 读也是一样需要文件包含O_RDONLY
- 整个流程如下
- //打开文件
- //读前设置源文件当前位置
- //写前设置目标文件当前位置
- //读源文件
- //写目标文件
- //完成文件复制后关闭两个文件
- #endif
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define buf_size 1024
- unsigned char buf[buf_size];
- int main(void)
- {
- int src, dst;
- src = open("./src", O_CREAT|O_RDONLY, S_IRWXU|S_IRWXG|S_IROTH);
- dst = open("./dst", O_CREAT|O_TRUNC|O_RDONLY|O_WRONLY, S_IRWXU|S_IRWXG|S_IROTH);
- if(src<0 |dst<0)
- {
- printf("fail to open files");
- exit(1);
- }
- lseek(src, 0, SEEK_SET);
- ssize_t real_read;
- while((real_read = read(src,(void*)buf,(size_t)buf_size)) >0)
- {
- if(write(dst, (const void*)buf, (size_t)real_read)<0)
- {
- printf("write\n");
- exit(1);
- }
- }
- if(real_read == -1)
- {
- printf("read\n");
- exit(1);
- }
- close(src);
- close(dst);
- return 0;
- }