首页 > 学院 > 开发设计 > 正文

[Linux] 复制一个文件的内容到另一个文件

2019-11-06 06:30:56
字体:
来源:转载
供稿:网友

Operating System Concept》 2.26

#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>#include <unistd.h>#define MAX_FILENAME_LEN 1000#define MAX_BUF_LEN 150int main() { int fd1, fd2, n; char filename[MAX_FILENAME_LEN]; char buf[MAX_BUF_LEN]; char command[MAX_FILENAME_LEN+50];L1: PRintf("Source File: "); scanf("%s", filename); if ((fd1 = open(filename, O_RDONLY))<0) { puts("Open source file error!"); goto L1; } printf("Destination File: "); scanf("%s", filename); while ((fd2 = open(filename, O_WRONLY|O_CREAT))<0) ; while ((n = read(fd1, buf, MAX_BUF_LEN))>0) write(fd2, buf, n); if (n < 0) { puts("Copy error!"); exit(1); } sprintf(command, "less %s", filename); system(command); return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表