源文件复制到目标文件步骤:
1.打开源文件
2.打开目标文件
3.读取源文件
4.将读取的目标文件写入源文件
5.关闭源文件和目标文件
命令使用方式
./filecp s.txt ./t.txt
int
main(int
argc,char**
argv) //argc 记录命令个数
argv 记录命令输入内容
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
int main(int argc,char** argv)
{
int count=0;
int fd1=0;
int fd2=0;
char buf[512];
/*1.打开源标文件*/
fd1=open(argv[1],O_RDWR );
/*2.打开目标文件*/
fd2=open(argv[2],O_RDWR | O_CREAT,0777);
/*3.读取源文件数据*/
while((count=read(fd1,buf,512))>0)
/*4.将数据写入目标文件*/
if(count<=512)
{
write(fd2,buf,count);
}
else
{
write(fd2,buf,512);
}
/*5.关闭文件*/
close(fd1);
close(fd2);
return 0;
}