作业
使用write和read完成文件的拷贝。
将1.txt文件内容拷贝到2.txt中
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(argc != 3)
{
printf("外部传参错误\n");
return -1;
}
int fd1;
fd1 = open(argv[1],O_RDONLY);
if(fd1 == -1)
{
perror("open");
return -1;
}
int fd2;
fd2 = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC,0664);
if(fd2 == -1)
{
perror("open");
return -1;
}
char buff[5];
while((read(fd1,buff,sizeof(buff)))!=0)
{
write(fd2,buff,sizeof(buff));
}
printf("拷贝成功\n");
close(fd1);
close(fd2);
return 0;
}
运行结果:
知识梳理