易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Linux C编程连载
Linux C编程连载——cp的实现:
/**********************************************************
* This program is use to copy src_file to dest_file
* 1 Execute gcc -o copy copy.c
* 2 then, copy the execute file "copy" to the /usr/bin
* You can use command like this : copy src_file dest_file
* Author : Tan De
* Time : 2011-04-04
*********************************************************/
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<sys
/stat.h
>
#include
<sys
/types.h
>
#include
<fcntl.h>
#define BUFF_SIZE 1024
int main(int argc, char *argv[]){
int src_file,dest_file;
int real_read_len;
unsigned char buff[BUFF_SIZE];
//argc is not correct
if(argc!=3){
printf("Error use copy! ");
printf("Example: ");
printf("copy src_file dest_file ");
exit(1);
}
//Open src_file read only
src_file
=
open
(argv[1],O_RDONLY);
//If the dest_file is not exsit, then create new one
dest_file
=
open
(argv[2],O_WRONLY|O_CREAT,666);
//Open error
if(src_file
<0
||dest_file
<0
){
printf("Open file error ");
printf("Can"t copy! ");
printf("Please check cmd : copy src_file dest_file ");
exit(1);
}
//Copy src_file to dest_file
while((
real_read_len
=read(src_file,buff,sizeof(buff)))
>
0){
write(dest_file,buff,real_read_len);
}
//close fd
close(dest_file);
close(src_file);
return 0;
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图