Linux系统编程 41 -read和write实现copy

这篇博客介绍了如何利用Linux系统调用read和write实现文件复制的功能。通过编写C语言程序mycopy.c,读取源文件并写入目标文件,同时展示了如何处理可能出现的错误。程序中包含了错误检查,并使用perror函数输出错误信息。此外,还提供了makefile文件用于编译程序。
摘要由CSDN通过智能技术生成

Linux系统编程 41 read和write实现cp

学习笔记

ssize_t read(int fd, void *buf, size_t count);
ssize_t中的s表示有符号

参数:
第一个:文件描述符
第二个:存放数据的缓冲区
第三个:缓冲区的大小

返回值:
1.成功的时候,返回实际读到的字节数(0:指示已经到达文件结尾了EOF)
2.失败的时候,返回-1,errno会被赋值相应的值。


#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
第一个:文件描述符
第二个:存放数据的缓冲区,待写入文件的原材料
第三个:实际要写的内容的大小 注意和read区别

返回值
1.成功的时候,返回实际写入的字节数
2.失败的时候,会返回-1


实现拷贝的操作

$cat mycopy.c 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<unistd.h> //#unix stdandard 
#include<errno.h>
#include<fcntl.h>
int main(int argc, char *argv[])
{
    int fd1 = open(argv[1],O_RDONLY);
    int fd2 = open(argv[2],O_RDWR|O_CREAT|O_TRUNC);

    char buff[1024];
    int n;
   while ( 0 !=(n= read(fd1,buff,1024)))
{
    write(fd2,buff,n);
}
    close(fd1);
    close(fd2);
    return 0;
}


makefile文件(将每一个.c文件都生成各自的可执行文件)为

$cat makefile 
src=$(wildcard ./*.c) 
target=$(patsubst %.c,%,$(src)) 
myArgs= -Wall -g
All:${target}
%:%.c
    gcc  $< -o $@ $(myArgs)


clean:
    -rm -rf $(target) 
.PHONY: clean All


$make mycopy 的目的
makefile中将mycopy去替换target,
如果有多个文件的时候,只编译一个文件。

$ls
makefile  mycopy.c
$make mycopy
gcc  mycopy.c -o mycopy -Wall -g

$./mycopy makefile makefile.copy
$ls
makefile  makefile.copy  mycopy  mycopy.c

查看copy文件的内容。

$ll
total 32
drwxrwxr-x  2 ubuntu ubuntu 4096 Dec 14 22:41 ./
drwxrwxrwx 12 root   root   4096 Dec 14 22:07 ../
-rw-rw-r--  1 ubuntu ubuntu  220 Dec 14 22:38 makefile
--w--wx---  1 ubuntu ubuntu  220 Dec 14 22:41 makefile.copy*
-rwxrwxr-x  1 ubuntu ubuntu 9996 Dec 14 22:39 mycopy*
-rw-rw-r--  1 ubuntu ubuntu  920 Dec 14 22:17 mycopy.c
$chmod u+r makefile.copy 
$cat makefile.copy 
src=$(wildcard ./*.c) 
target=$(patsubst %.c,%,$(src)) 
myArgs= -Wall -g
All:${target}
%:%.c
    gcc  $< -o $@ $(myArgs)


clean:
    -rm -rf $(target) 
.PHONY: clean All


系统调用几乎都要去检查返回值。
perror函数用于输出错误。
void perror(const char*s);


改写完善下程序:

$cat mycopy.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<unistd.h> //#unix stdandard 
#include<errno.h>
#include<fcntl.h>
int main(int argc, char *argv[])
{
    int fd1 = open(argv[1],O_RDONLY);
    int fd2 = open(argv[2],O_RDWR|O_CREAT|O_TRUNC);

    char buff[1024];
    int n;
    if (-1 == fd1)
    {
        perror("open argv1 fail");
        exit(1);
    }
    if (-1 == fd2)
    {
        perror("open argv2 fail");
        exit(1);
    }
    while ( 0 !=(n= read(fd1,buff,1024)))
    {
        if(n<0)
        {
            perror("read error!");
        }
        write(fd2,buff,n);
        break;
    }
    close(fd1);
    close(fd2);
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值