【C语言】【unix c】编写代码实现cat的功能

来源:互联网 发布:s7200模拟量编程实例 编辑:程序博客网 时间:2024/05/19 09:15
编写代码实现cat的功能,编译成可执行文件名为pcat(pcat.c)    #include <stdio.h>    #include <sys/types.h>        #include <sys/stat.h>        #include <fcntl.h>        #include <unistd.h>    int main(int argc, char *argv[]) {    int fd;    int r;    char buf[128];    fd = open(argv[1], O_RDONLY);    if(fd == -1) {        perror("open");        return -1;        }    while((r = read(fd, buf, 128)) > 0) {        write(STDOUT_FILENO, buf, r);        }        close(fd);        return 0;    }
原创粉丝点击