实验要求
使用系统调用(
open
,read
,write
,close
等函数)编写文件拷贝程序.
实验环境
Linux
或Max OS X
实验代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char** argv){
if(argc != 3){
fprintf(stderr, "You must need a source file and destination file\n");
exit(0);
}
int so = open(argv[1], O_RDONLY|O_CREAT, 0644);
int de = open(argv[2], O_WRONLY|O_CREAT, 0644);
char c;
while(read(so, &c, 1)){
write(de, &c, 1);
}
close(so);
close(de);
printf("copy over\n");
return 0;
}