c语言 linux下文件i/o 实现文件的复制
#include#include#include#include#include#define N 64int main(int argc,char *argv[]){ int fds,fdt,n; char buf[N]; if(argc { printf("usage : %s \n",argv[0
·
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
#define N 64
int main(int argc,char *argv[])
{
int fds,fdt,n;
char buf[N];
if(argc < 3)
{
printf("usage : %s <src_file><dst_file>\n",argv[0]);
return -1;
}
if((fds = open(argv[1],O_RDONLY))==-1)//证明没打开
{
fprintf(stderr,"open %s :%s\n",argv[1],strerror(errno));//标准错误流中传信息
return -1;
}
if((fdt = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0666))==-1)//证明没打开
{
fprintf(stderr,"open %s :%s\n",argv[2],strerror(errno));//标准错误流中传信息
return -1;
}
while((n =read(fds,buf,N))>0)
{
write(fdt,buf,n);
}
close(fds);
close(fdt);
return 0;
}
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
#define N 64
int main(int argc,char *argv[])
{
int fds,fdt,n;
char buf[N];
if(argc < 3)
{
printf("usage : %s <src_file><dst_file>\n",argv[0]);
return -1;
}
if((fds = open(argv[1],O_RDONLY))==-1)//证明没打开
{
fprintf(stderr,"open %s :%s\n",argv[1],strerror(errno));//标准错误流中传信息
return -1;
}
if((fdt = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0666))==-1)//证明没打开
{
fprintf(stderr,"open %s :%s\n",argv[2],strerror(errno));//标准错误流中传信息
return -1;
}
while((n =read(fds,buf,N))>0)
{
write(fdt,buf,n);
}
close(fds);
close(fdt);
return 0;
}
更多推荐
已为社区贡献1条内容
所有评论(0)