作業系統實驗之檔案拷貝
阿新 • • 發佈:2019-02-08
實驗要求
使用系統呼叫(
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;
}