linux ,编写一个程序,把一个文件复制到另一个文件上

即实现简单的copy功能
要求:只用 open() read() write() 和close()系统调用.
程序第一个是源文件.第二个参数是目的文件
-------
其实就是 linux教程 第2版 第7章 课后思考第3题

我刚刚学,不是很懂啊.大虾矿帮帮我~急

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUF_SIZE 1024*8
int main()
{
int fds, fdd;
char buf[BUF_SIZE];
size_t hasread = 0;
fds = open("filea", O_RDONLY);
fdd = open("fileb", O_WRONLY, O_CREAT);
if(fds && fdd)
{
while((hasread = read(fds, buf, sizeof(buf))) > 0)
{
write(fdd, buf, hasread);
}
close(fds);
close(fdd);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-03-07
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<sys/types.h>
#include
<sys/stat.h>
#include
<fcntl.h>
#define
BUF_SIZE
1024*8
int
main()
{
int
fds,
fdd;
char
buf[BUF_SIZE];
size_t
hasread
=
0;
fds
=
open("filea",
O_RDONLY);
fdd
=
open("fileb",
O_WRONLY,
O_CREAT);
if(fds
&&
fdd)
{
while((hasread
=
read(fds,
buf,
sizeof(buf)))
>
0)
{
write(fdd,
buf,
hasread);
}
close(fds);
close(fdd);
}
}
相似回答