系统调用,将一个文件复=中的内容复制到另一个文件中去

来源:互联网 发布:奥维互动地图mac版 编辑:程序博客网 时间:2024/05/16 07:39
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <errno.h>#define BUFFER_SIZE 1024int main(int argc,char *argv[]){    if(argc != 3)    {        printf("please input two file:\n");exit(1);    }    int from_fd;    int to_fd;    char buffer[BUFFER_SIZE];    from_fd = open(argv[1],O_RDONLY);    if(-1 == from_fd)    {        perror("open from_file error");exit(1);    }    to_fd = open(argv[2], O_CREAT | O_WRONLY, 0655);    if(-1 == to_fd)    {        perror("open to_file error!");exit(1);    }    while((read_line(from_fd,buffer,sizeof(buffer))) != -1)    {        if((write(to_fd,buffer,strlen(buffer))) == -1){    perror("write to_file error");    exit(1);}if((write(to_fd,"\n",1)) == -1){    perror("write data n error");    exit(1);}memset(buffer,0,sizeof(buffer));    }    return 0;}


0 0