嵌入式课堂小测试(二)

时间:2022-12-01 14:39:59

编写一程序,实现把一个文件的内容复制到另一个文件中的功能。可以参照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;

}