补全代码:#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #define resource_mode 0 #define destination_mode 0774 #define FILESIZE 1024 int main(int argc, char *argv[]) { int resource_fd, destination_fd; char buffer[FILESIZE], *p; int readbytes, writebytes; if (argc != 3) { printf("Usage:copy from resource file to destination file\n %s src_file dest_file\n", argv[0]); exit(0); } if ((resource_fd = open(argv[1], resource_mode)) == -1) { perror("Can't open source file"); exit(0); } if ((destination_fd = creat(argv[2], destination_mode)) == -1) { perror("Can't create destination file"); exit(0); } // 请使用read函数读取前1024字节的内容读到缓冲区buffer中 while (readbytes = read()) { p = buffer; if ((readbytes == -1) && (errno != EINTR)) break; else if (readbytes > 0) { // 请使用write函数读取到的前1024字节的内容写到目的文件中 while (writebytes = write()) { if ((writebytes == -1) && (errno != EINTR)) break; else if (writebytes == readbytes) break; else if (writebytes > 0) { p += writebytes; readbytes -= writebytes; } } if (writebytes == -1) break; } } close(resource_fd); close(destination_fd); return 0; }
时间: 2024-04-07 17:29:11 浏览: 24
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define resource_mode 0
#define destination_mode 0774
#define FILESIZE 1024
int main(int argc, char *argv[]) {
int resource_fd, destination_fd;
char buffer[FILESIZE], *p;
int readbytes, writebytes;
if (argc != 3) {
printf("Usage:copy from resource file to destination file\n %s src_file dest_file\n", argv[0]);
exit(0);
}
if ((resource_fd = open(argv[1], resource_mode)) == -1) {
perror("Can't open source file");
exit(0);
}
if ((destination_fd = creat(argv[2], destination_mode)) == -1) {
perror("Can't create destination file");
exit(0);
}
// 请使用read函数读取前1024字节的内容读到缓冲区buffer中
while ((readbytes = read(resource_fd, buffer, FILESIZE)) > 0) {
p = buffer;
// 请使用write函数读取到的前1024字节的内容写到目的文件中
while ((writebytes = write(destination_fd, p, readbytes)) > 0) {
if (writebytes == readbytes)
break;
else if (writebytes > 0) {
p += writebytes;
readbytes -= writebytes;
}
}
if (writebytes == -1)
break;
}
close(resource_fd);
close(destination_fd);
return 0;
}
相关推荐
![application/x-bzip2](https://img-home.csdnimg.cn/images/20210720083646.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)