惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
WordPress大学
WordPress大学
D
DataBreaches.Net
腾讯CDC
小众软件
小众软件
B
Blog RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Y
Y Combinator Blog
V
V2EX
I
InfoQ
D
Docker
量子位
N
Netflix TechBlog - Medium
Recent Announcements
Recent Announcements
A
About on SuperTechFans
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog
阮一峰的网络日志
阮一峰的网络日志
MyScale Blog
MyScale Blog

博客园 - wolflion

《UNIX-Shell编程24学时教程》读书笔记Chap3,4 文件,目录操作 《UNIX-Shell编程24学时教程》读书笔记Chap1,2 Shell基础,脚本基础 《UNIX-Shell编程24学时教程》读书笔记chap7 变量 《软件调试的艺术》读书笔记 ubuntu环境准备 ftp的实现 icmp的程序(ping的实现) who命令 苦逼IT才能看懂的笑话 debug和release版区别 i5处理器的台式机[百度知道] 关于轮胎尺寸问题 常见内核数据结构.doc windows 系统编程 Chap7 线程和调度 EVRYTHNG.H Windows系统编程chap6 Windows系统编程 chap5 booklist 转 windows code
cp命令
wolflion · 2013-07-23 · via 博客园 - wolflion

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#define BUFFERSIZE 4096
#define COPYMODE 0644

void oops(char *, char *);

main(int ac, char *av[])
{
int in_fd, out_fd, n_chars;
char buf[BUFFERSIZE];

if ( ac != 3) //check args
{
fprintf(stderr, "usage: %s source destination\n", *av);
exit(1);
}

//open files
if ((in_fd = open(av[1], O_RDONLY)) == -1)
oops("Cannot open", av[1]);

if((out_fd = create(av[2], COPYMODE)) == -1)
oops("Cannot creat", av[2]);

//copy files
while ((n_chars = read(in_fd, buf, BUFFERSIZE)) > 0)
if (write(out_fd, buf, n_chars) != n_chars)
oops("Write error to", av[2]);

if(n_chars == -1)
oops("Read error from ", av[1]);

//close files
if (close(in_fd) == -1 || close(out_fd) == -1)
oops("Error closing files","");
}

void oops(char *s1, char *s2)
{
fprintf(stderr, "Error: %s", s1);
perror(s2);
exit(1);
}