close
unix c 將檔案從 123 複製到 cp123
使用的是 read 和 write 而不是 fread 和 fwrite
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
char s[50];
int infile;
int outfile;
int count;
infile = open("./123", O_RDONLY);
outfile = open("./cp3", O_WRONLY|O_CREAT|O_TRUNC,0755);
if (infile < 0) {
perror("open");
exit(1);
}
while (1) {
count = read(infile, s, sizeof s);
if (count <= 0) {
break;
}
write(outfile, s, count);
}
close(outfile);
return 0;
}
全站熱搜