cp src.c des.c
指令 源文件 目标文件
1.C语言参数
2.思路:
a.打开src.c
b.读src到buf
c.打开/创建des.c
d.将buf写入到des.c
e.close两个文件
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
int fdSrc;
int fdDes;
char *readBuf = NULL;
if(argc != 3){
printf("pararm error\n");
exit(-1);
}
fdSrc = open(argv[1],O_RDWR);
int sizeReadBuf = lseek(fdSrc,0,SEEK_END); //计算缓冲区的大小
lseek(fdSrc,0,SEEK_SET); //将光标定位到文件头部,防止读取不到文件
readBuf = (char *)malloc(sizeof(char *)*sizeReadBuf+8);
int n_read = read(fdSrc,readBuf,sizeReadBuf);
fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
int n_write = write(fdDes,readBuf,sizeReadBuf);
close(fdSrc);
close(fdDes);
printf("mycp success\n");
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)