《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;}新闻热点
疑难解答