日期:2014-05-16 浏览次数:21039 次
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int openfile(const char * filename)
{
    int fd;
    fd=open(filename,O_RDONLY);
    if(fd==-1) printf("open err:源文件%s不存在:%m\n",filename), exit(-1);
    return fd;
}
int openfile2(const char * filename)
{
    int fd;
    fd=open(filename,O_RDWR|O_CREAT|O_EXCL,0666);
    if(fd==-1) printf("file exist!\n");
    return fd;
}
void save(int fd,int fd2)
{
    char buf[20];
    int n;
    bzero(buf,sizeof(buf));
    while((n=read(fd,buf,20))>0)
    {
        write(fd2,buf,n);
    }
}
int main(int args,char**argv)
{
    int fd;
    int fd2;
    
    fd=openfile(argv[1]);
    fd=openfile2(argv[2]);
    
    save(fd,fd2);
    
    close(fd);
    close(fd2);
    return 0;
}