• linux中实现文件的复制(c代码实现)




    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #include<unistd.h>
    int main(){ 
            //首先打开一个文件
            int file=open("sin.txt",O_RDONLY);
            if(file==-1){//校验是否成功的打开了文件
            perror("read");//打开失败输出信息
            exit(1);//失败退出当前的程序
            } 
            //创建一个写入的文件
            int new_file=open("love.txt",O_CREAT|O_WRONLY,0777);//设置权限为777的love的txt文件
            if(new_file==-1){//校验是否成功的创建了该文件 
            perror("write");//打开失败输出信息
            exit(1);//失败退出当前的程序
            } 
            //创建一个缓冲,初始化为一
            int buff[1024]={0};
            int count=0;//初始化计数器
            count=read(file,buff,1024);//将读到的字节数组保存到缓冲数组中
            if(count==-1){
            perror("read");//文件中没有数据
            exit(1);//退出程序
            }
            while(count){//当输出为0时读取完毕
            write(new_file,buff,count);//将读取到的字节写入到文件中
            count=read(file,buff,count);//继续进行读取
            }
            close(file);//关闭文件
            close(new_file);//关闭文件
    }
  • 相关阅读:
    CSS文字的处理
    typeof 检测变量的数据类型
    BZOJ 1257: [CQOI2007]余数之和
    BZOJ 1218: [HNOI2003]激光炸弹
    BZOJ 3251: 树上三角形
    BZOJ 3916: [Baltic2014]friends
    BZOJ 1610: [Usaco2008 Feb]Line连线游戏 暴力
    BZOJ 1593 [Usaco2008 Feb]Hotel 旅馆 双倍经验,线段树
    BZOJ 1096 [ZJOI2007]仓库建设 BZOJ 3437 小P的牧场 BZOJ 3156 防御准备 斜率优化dp
    BZOJ 2582 : Bovine Alliance DFS
  • 原文地址:https://www.cnblogs.com/csnd/p/16675701.html
Copyright © 2020-2023  润新知