1.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
int main(int argc, char* argv[])
{
if ( argc != 3 )
{
printf("arg err\n");
return 0;
}
char * filename = argv[1];
char * newfilename = argv[2];
int fdr = open(filename,O_RDONLY);
int fdw = open(newfilename,O_WRONLY|O_CREAT,0600);
if ( fdr == -1 || fdw == -1 )
{
printf("open file failed\n");
return 0;
}
char buff[512] = {
0};
int n = 0;
while( ( n = read(fdr,buff,512)) > 0 )
{
write(fdw,buff,n);
}
close(fdr);
close(fdw);
/*
int fd = open("a.txt",O_RDONLY);
assert( fd != -1 );
char buff[128] = {0};
int n = read(fd,buff,127);// read() == 0
printf("buff=%s,n=%d\n",buff,n);
close(fd);
*/
/*
int fd = open("a.txt",O_WRONLY|O_CREAT,0600);
assert( fd != -1 );
write(fd,"hello",5);
close(fd);
*/
}
7_24
Fork复制进程
僵死进程:子进程先于父进程结束,父进程没有获取子进程的退出码。
文件操作调用:open,read,write,close
Man:1 命令 2 系统调用 3 库函数
Linux 不区分二进制和文本文件,特殊字符
替换进程:exec系列