嵌入式课堂小测试(二)

编写一程序,实现把一个文件的内容复制到另一个文件中的功能。可以参照Linuxcp 命令所实现的功能来实现。

程序源代码如下:(自行编写并调试通过,请勿复制上课时所提供的代码)

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#define MAX_SIZE 2048

int main(int argc, char * argv[])

{

    int in_fd;

    int out_fd;

    int chars;

    char buf[MAX_SIZE] = {0};

    if(argc != 3)    //入口参数检查

    {

        printf("please input a!\n");

        exit(1);

    }

    in_fd = open(argv[1],O_RDONLY);

    if(in_fd == -1)

    {

        printf("open file error!\n");

        exit(1);

    }

    out_fd = creat(argv[2],0644);

    if(out_fd == -1)

    {

        printf("create file error!\n");

        exit(1);

    }

    chars = read(in_fd,buf,MAX_SIZE);

    if(chars == -1)

    {

        printf("read file error!\n");

        exit(1);

    }

    if(write(out_fd,buf,chars) != chars)

    {

        printf("write file error!\n");

        exit(1);

    }

    close(in_fd);

    close(out_fd);

    return 0;

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值