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

xiaoxiao2021-02-27  352

#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);//关闭文件 }
转载请注明原文地址: https://www.6miu.com/read-925.html

最新回复(0)