Skip to content

庄朋龙的博客

有志者自有千方万计,无志者只感千难万难。

Menu
  • 首页
  • 生活
  • 创业
  • 编程
  • 运维
  • 视频课程
    • 跨境独立站
    • Shopee跨境电商
    • TEMU跨境电商
  • 资源分享
    • 网站推荐
    • 开源推荐
    • 可商用字体
    • 书籍分享
Menu

Linux系统函数_read和write实现文件拷贝功能 代码

Posted on 2015年11月24日2025年3月4日 by 庄朋龙

Linux系统函数_read和write实现文件拷贝功能 代码,练习所写不足之处请在下方评论指出

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>

#define N 1024

int main(int argc, char const* argv[])
{
    int fd, fd_out;
    int n;
    char buf[N];

    fd = open(argv[1], O_RDONLY);
    if(fd < 0)
    {
        perror("open 1 error");
        exit(1);
    }

    fd_out = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644);
    if(fd_out < 0)
    {
        perror("open 2 error");
        exit(1);
    }
    while((n = read(fd, buf, N)))
    {
        if(n < 0)
        {
            perror("read error");
            exit(1);
        }
        write(fd_out, buf, n);
    }
    close(fd);
    close(fd_out);
    return 0;
}

 

Category: 资源分享

全栈开发者×创业偏执狂

相信代码能改变命运,也相信凌晨四点的服务器警报里有真实的人生。

我的创业项目:
SHOPAGG / SmallShop / DigitShops / 文硕阁

© 2025 庄朋龙的博客